.htaccess htaccess从url删除扩展名可以工作,但页面不';t载荷

.htaccess htaccess从url删除扩展名可以工作,但页面不';t载荷,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我设法使/about.php改为/about with 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] 但是现在页面无法加载。为什么? 非常感谢您的帮助 更新: Re

我设法使/about.php改为/about with

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]

您已经可以探测
.php
的传入请求,并在不使用
.php
的情况下重定向浏览器,但是您仍然需要另一条规则来捕获不使用
.php
的后续URI,并使用
.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
这样的请求,如果
/something.php
实际上不存在,则仍将返回404,但是
/something
中存在
/something.php
/something.php
将静默执行
/something.php
,而不更改
/something>中的浏览器地址栏

现在,即使文件实际上不存在,上面的代码也会删除
.php
,因此对
/notexist.php
的请求会将用户重定向到
/notexist
,这将导致404。如果您甚至不想在
/notexist.php
上将它们重定向到只返回404,您可以执行以下操作以确保它立即返回404:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
更新:订单事宜。 看到其他规则已经就位,这些规则对它们的执行顺序非常敏感,特别是因为您有一个全面的
RewriteRule^(.*)$/test/profile.php?username=$1

RewriteBase /

RewriteEngine On

# This removes /index.php, do this first
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

# Do the .php removal next
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

# /pages is specific, so do it next
RewriteRule ^pages/(.*)$ /$1 [L,NC,R]

# Only do this if the .php exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]

# Then the more generic one to write username
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/profile.php?username=$1 [L]

我已经测试了所有这些,并在自己的环境中进行了验证。

请查看我的更新。它仍然不工作,页面确实存在,因此它应该加载订单事项!当您寻求mod_rewrite帮助时,请始终发布所有规则。等等…@Gadgetster好的,我用一种更符合逻辑的方式对它们重新排序这仍然会让我进入一个错误404页面,尽管我有about.php和其他页面。。本地主机上的im,如果这有区别的话。您从哪个确切的URL开始,以及
about.php
在哪里存在?您的规则意味着它存在于根“/about.php”处。此外,浏览器可能会主动缓存重定向。用不同的浏览器测试它。