简体   繁体   English

.htaccess-隐藏.php和强制尾部斜杠不起作用

[英].htaccess - hidding .php and forcing trailing slash not working

I'm trying to hide .php extension from my website while forcing trailing slash, I've been searching for the last few days with no success. 我试图在我的网站中隐藏.php扩展名,同时强制使用斜杠,但我一直在搜索最近几天,但均未成功。

The site is running in a XAMPP server currently. 该站点当前正在XAMPP服务器中运行。

Here's the .htaccess file: 这是.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/

These links work: 这些链接有效:

localhost/about-us redirects to localhost/about-us/ which is correct but when I try to access localhost/about-us.php , it redirects to localhost/about-us/ instead of localhost/about-us/ . localhost/about-us重定向到localhost/about-us/是正确的,但是当我尝试访问localhost/about-us.php ,它将重定向到localhost/about-us/而不是localhost/about-us/ (sorry can't post links) (抱歉无法发布链接)

How can this be fixed? 如何解决?

You just seem to have accidentally used an absolute path. 您似乎只是不小心使用了绝对路径。 Just remove the slash at the beginning of /$1/ like your other rules to make it work properly. 只需像其他规则一样删除/$1/开头的斜杠即可使其正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM