Php Combine.htaccess规则:删除扩展名并重写URL变量

Php Combine.htaccess规则:删除扩展名并重写URL变量,php,.htaccess,url-rewriting,vanity-url,Php,.htaccess,Url Rewriting,Vanity Url,我有一个.htaccess文件,其中包含以下规则 # replace 'RewriteBase' with current working folder # put '<base href="/">' in <head> if you are in the root folder # put '<base href="/foldername/">' in <head> if you are in a subfolder of the root Op

我有一个
.htaccess
文件,其中包含以下规则

# replace 'RewriteBase' with current working folder
# put '<base href="/">' in <head> if you are in the root folder
# put '<base href="/foldername/">' in <head> if you are in a subfolder of the root
Options +FollowSymLinks
RewriteEngine On
RewriteBase /pretty-urls

# Remove slash if file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]

# Redirect to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]

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

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ user.php?u=$1 [NC]
非常感谢您的帮助,谢谢

编辑:Apache错误日志显示

[Fri Sep 27 14:26:26.539589 2013] [core:error] [pid 2276:tid 1680] [client ::1:60023] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://localhost/pretty-urls/users

正如建议的那样,我删除了
[NC]
标志并添加了
[L]
标志,但相同的错误仍然存在。

错误
请求超过了10个内部重定向的限制
通常是由既匹配原始url又匹配重写url的重写规则引起的。
L
标志只会停止当前周期的重写,但由于apache再次将请求推送到.htaccess,这不会停止这些错误的内部重定向

在我自己的本地主机上重新创建时,我注意到问题在于将
.php
添加到url的规则

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
%{REQUEST\u FILENAME}
在我的例子中包含指向
文档根/user
的文件路径。测试
document\u root/user.php
是否是一个文件(确实是),然后将
.php
添加到url,这将导致
/user/john/.php
。我认为这个问题可以通过将文件移动到子目录来解决,这样虚拟url就永远不会与文件夹中的文件或目录匹配

您可以将规则更改为以下内容,但我不确定这是否会完全消除问题:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?(.*)$ $1.php?q=$2 [L]
这将重写
link.url/test/qwer
test.php?q=qwer
link.url/test
test.php?q=

user.php
进行内部重写的规则应移到“常规”规则之上,并应更改为:

RewriteRule ^user/(.*)/?$ user.php?u=$1 [NC,L]
这将把
link.url/user/john/
重写为
user.php?u=john

一如既往,请参见。要正确调试
.htaccess
,请将
日志级别警报重写:trace3
放入
httpd.conf
并重新启动服务。这将显示如下所示的线条,为您提供正在发生的事情的线索:

[rewrite:trace1] [pid 4800:tid 1532] mod_rewrite.c(468): [client ::1:49451] ::1 - - [localhost/sid#3ecb48][rid#12b1a88/initial/redir#2] [perdir C:/wamp/www/] internal redirect with /user/john/.php.php.php [INTERNAL REDIRECT]

尝试使用Apache日志调试您的重写脚本,如下所示:谢谢,我检查了答案并更新了我的帖子,但仍然有500个错误。您在
无扩展url
规则中缺少
]
。抱歉,这是一个输入错误,在文件中是正确的。您实际解决了这个问题,不知道如何感谢您。我倒转了最后两个街区,现在一切正常了。再次感谢你!
[rewrite:trace1] [pid 4800:tid 1532] mod_rewrite.c(468): [client ::1:49451] ::1 - - [localhost/sid#3ecb48][rid#12b1a88/initial/redir#2] [perdir C:/wamp/www/] internal redirect with /user/john/.php.php.php [INTERNAL REDIRECT]