避免重复的URL与我的mod_write [英] avoiding duplicate urls wih my mod_write
问题描述
此刻,我有一个简单的.htaccess来整理页面网址,方法是将.php删除掉或添加,具体取决于您查看它的方式.
At the moment, I have a simple .htaccess that tidies up my page urls, by knocking the .php off the end, or adding it, depending on which way you look at it.
基本上,/about.php将变为/about.
Basically, /about.php will become /about.
但是,这导致在这两个URL上都可以访问我的页面.我可以将其吸干并使用rel = canonical标签,但我也想在htaccess中对其进行整理.
However, this results in my page being accessible on both of those urls. I can just suck it up and use rel=canonical tags but I'd also like to tidy it in my htaccess.
除了手动指定每个url之外,是否还有更好的方法(聪明的规则)将301的php转换为无php的版本?
Is there a better way (clever rule) to 301 the .php to the none .php version, other than manually specifying each url?
还,我真的需要那些rewritecond规则吗?
Also, do I really need those rewritecond rules?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [QSA,L]
ErrorDocument 404 /404
</IfModule>
推荐答案
要将每个.php-URL重定向(301)到非.php版本(如果已存在),则可以使用以下版本:>
To redirect (301) each .php-URL to the non-.php version (in case it's an existing file) you could use this one:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \.php\ HTTP/.\..$
RewriteRule ^(.*)\.php$ /$1 [QSA,R=301,L]
.haccess中的前两个RewriteCond并不是真正需要的,因为无论如何您都会遇到一些冲突,例如如果您有文件foo.htm
和foo.htm.php
,则在请求foo.htm
时应显示哪个文件?
The first two RewriteCond in your .haccess are not really required, because you will have some conflicts anyway, e.g. if you have a file foo.htm
and foo.htm.php
, which one should be shown when foo.htm
is requested?
因此,完整的mod_rewrite
块:
RewriteEngine On
# redirect /foo.php to /foo
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \.php\ HTTP/.\..$
RewriteRule ^(.*)\.php$ /$1 [QSA,R=301,L]
# rewrite /foo to /foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [QSA,L]
这篇关于避免重复的URL与我的mod_write的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!