分组文件扩展名重写URL的[的.htaccess] [英] Grouping file extensions for re-writing URL's [.htaccess]

查看:173
本文介绍了分组文件扩展名重写URL的[的.htaccess]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能将这些两者结合起来;是像(HTML | PHP)。$ - 请提供code

请注意,答案应该适用于所有的文件扩展名:什么是你的想法就会删除文件扩展名;所使用的code注释 - 因为它是罕见的。

 的RewriteCond%{THE_REQUEST} ^ GET \(。*)\。HTML \ HTTP
重写规则(。*)\。HTML $ $ 1 [R = 301]
的RewriteCond%{} REQUEST_FILENAME html的-f
的RewriteCond%{REQUEST_URI}!/ $
重写规则(。*)$ 1 \ html的[L]

的RewriteCond%{THE_REQUEST} ^ GET \(。*)\。PHP \ HTTP
重写规则(。*)\。PHP的$ $ 1 [R = 301]
的RewriteCond%{} REQUEST_FILENAME -f .PHP
的RewriteCond%{REQUEST_URI}!/ $
重写规则(。*)$ 1 \ .PHP [L]
 

解决方案

不幸的是,这是不可能的。

的TestString 的一部分的RewriteCond 只能包含一个简单的字符串,并允许服务器变量(HTTP_REFERER code>,例如)和反向引用( $ 1 %1 ,例如)。

因此​​,你不能包括前pression在的TestString 的 - 只有的 CondPattern 的的(第二部分的RewriteCond )允许前pressions。

这是你能做的最好是使用EX pression来剔除你不想要的扩展名,然后,为你剥出每个扩展,确定是否存在,并相应地重写 - 就像这样:

RewriteEngine叙述上 #第1步:你不想为实际存在的文件去掉扩展 的RewriteCond%{THE_REQUEST} ^ GET \(。*)\。(HTML | PHP)\ HTTP 的RewriteCond%{} REQUEST_FILENAME -f 重写规则^%1 [R = 301,L] #第二步:剥离每个扩展,改写了现有文件 的RewriteCond%{} REQUEST_FILENAME html的-f 重写规则(。*)$ 1.HTML [L] 的RewriteCond%{} REQUEST_FILENAME -f .PHP 重写规则(。*)$ 1.PHP [L]

侧面说明:的这将是真正伟大如果Apache实现某种循环在的.htaccess 文件 - 这将有助于简化这种方法。不幸的是,这不是这种情况,并且因此上述是pretty的多的最佳方法。

How can I group these two together; is it like ".(html|php)$" - please provide the code.

Note, the answer should work for all file extensions: what is your thoughts on removing every file extension; comment on the code used - as it is rare to find.

RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]

RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

解决方案

Unfortunately, this is not possible.

The TestString part of RewriteCond can only contain a plain string, and allows for server variables (HTTP_REFERER, for example) and back-references ($1 and %1, for example).

As such, you cannot include an expression in the TestString - only the CondPattern (the second part of RewriteCond) allows for expressions.

The best that you can do is use an expression to strip out the extensions you don't want and then, for each extension you've stripped out, determine if it exists and rewrite accordingly - like so:

RewriteEngine on

# Step 1: Strip out extensions you don't want for files that actually exist

RewriteCond %{THE_REQUEST} ^GET\ (.*)\.(html|php)\ HTTP
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1 [R=301,L]

# Step 2: For each extension stripped, rewrite for existing files

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

Side Note: It would be really great if Apache implemented some sort of looping in .htaccess files - it would help simplify this methodology. Unfortunately, this is not the case, and so the above is pretty much the best method.

这篇关于分组文件扩展名重写URL的[的.htaccess]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆