.htaccess - 隐藏.php并强制尾随斜杠不起作用

时间:2015-05-18 11:57:41

标签: php apache .htaccess mod-rewrite

我正在尝试隐藏.php网址,同时强制使用斜杠,我一直在寻找最近几天但没有成功。

该网站目前正在XAMPP服务器上运行。

这是.htaccess文件:

Options -Indexes -Multiviews +FollowSymlinks

RewriteEngine On
RewriteBase /

#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

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

#forcing trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

ErrorDocument 404 http://localhost/404/

这些链接有效:

localhost/about-us重定向到localhost/about-us/这是正确的,但当我尝试访问localhost/about-us.php时,它会重定向到localhost/about-us/而不是localhost/about-us/。 (抱歉无法发布链接)

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您似乎偶然使用了绝对路径。只需删除/$1/开头的斜杠,就像其他规则一样,以使其正常工作。

#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ $1/ [L,R=301]
相关问题