我设法让/about.php改为/关于
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
但现在页面没有加载。为什么? 非常感谢任何帮助!
更新:
RewriteBase /
RewriteEngine On
# Do the .php removal first
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/profile.php?username=$1
# Only do this if the .php exists
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php [L]
RewriteRule ^pages/(.*)$ /$1 [L,NC,R]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
答案 0 :(得分:1)
您已经调查了.php
的传入请求,并在没有.php
的情况下重定向浏览器,但您仍然需要另一条规则来捕获后续URI而不使用{{1}并使用 .php
静默执行。
.php
RewriteEngine On
# Do the .php removal first
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
# And add a catch-all rule to point requests to .php silently
# if the file exists.
#
# Don't interfere with real file requests:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Only do this if the .php exists
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php [L]
/something
实际上不存在的请求仍会返回404,/something.php
/something
exists将默默执行/something.php
而不更改/something.php
的浏览器地址栏。
现在,即使文件实际上不存在,上述内容也会移除/something
,因此向.php
发出的请求会将用户重定向到/notexist.php
,这将导致404.如果您不想在/notexist
上重新定向那些只返回404,那么您可以执行以下操作以确保它立即返回404:
/notexist.php
看到已有的其他规则,这些规则对他们执行的订单非常敏感,特别是因为你有一个全能的RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteRule ^(.*)$ /test/profile.php?username=$1
我已经测试了所有这些并在我自己的环境中验证了它们。