将尾部斜杠重定向到无扩展名

时间:2014-02-28 04:19:24

标签: apache .htaccess mod-rewrite redirect

我的htaccess文件是:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^/?$ "http\:\/\/mydomain\.com\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

现在它的工作原理如下:

mydomain.com/contact.php (  redirect --> )   mydomain.com/contact
mydomain.com/contact/    (  redirect --> )   404 - not found:(
mydomain.com/contact     (  redirect --> )   mydomain.com/contact

如何将“mydomain.com/contact/”重定向到:“mydomain.com/contact”? 或者只是让浏览器打开正确的页面,而不是现在的404:/

1 个答案:

答案 0 :(得分:0)

使用此代码:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]