.htaccess没有尾部斜杠重定向到尾部斜杠

时间:2014-04-28 13:25:13

标签: regex apache .htaccess mod-rewrite

我在里面使用这个.htaccess:

DirectoryIndex index.php index.html
DirectorySlash On

Options -Indexes

Options +FollowSymlinks

RewriteEngine On
 RewriteBase /

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

RewriteCond %{REQUEST_URI} ^/(page1|page2|page3)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

#Show every html,htm and xml as php
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^(.*)\.htm$ $1.php [nc]
RewriteRule ^(.*)\.xml$ $1.php [nc]

但我没有将http://example.com/page1重定向到 - > http://example.com/page1/(以及其他第2页和第3页)。因为我在本地测试我的网址会是      本地主机/示例/第1页 并应重定向到      本地主机/示例/第1页/  有没有人看到任何问题?

1 个答案:

答案 0 :(得分:0)

要添加尾部斜杠,您可以使用以下规则:

DirectoryIndex index.php index.html
DirectorySlash On

Options -Indexes    
Options +FollowSymlinks

RewriteEngine On
RewriteBase /

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(page1|page2|page3)$ /$1/ [R=301,L,NC]

#Show every html,htm and xml as php
RewriteRule ^(.+?)\.(?:xml|html?)$ $1.php [NC,L]
相关问题