我的网站根目录中有一个.htaccess文件,其内容如下:
Options +SymLinksIfOwnerMatch -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^my-domain\.com
RewriteRule (.*) https://my-domain.com/$1 [R=301,L]
#Check if connection is HTTPS
RewriteCond %{HTTPS} !=on
#Redirect HTTP to HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^user\/([a-zA-Z0-9_\-\.]+)/?$ /profile.php?username=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^friends\/([a-zA-Z0-9_\-\.]+)/?$ /friends.php?username=$1 [L]
ErrorDocument 400 /errors/1.php
ErrorDocument 401 /errors/2.php
ErrorDocument 403 /errors/2.php
ErrorDocument 404 /errors/4.php
ErrorDocument 500 /errors/6.php
除了friends.php的重写规则之外的所有内容都有效。
如果我转到my-domain.com/user/example,我会成功获取my-domain.com/profile.php?username=example的内容。
但是,当我转到my-domain / friends / example时,它会给我一个内部服务器错误,我看到这是我的apache错误日志:
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.
在调试日志中,我得到以下几行:
[Sun May 18 00:42:12 2014] [error] [client 122.175.169.57] 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.
[Sun May 18 00:42:12 2014] [debug] core.c(3112): [client ###] r->uri = /friends/example.php.php.php.php.php.php.php.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php.php.php.php.php.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php.php.php.php.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php.php.php.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php.php.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example.php
[Sun May 18 00:42:12 2014] [debug] core.c(3118): [client ###] redirected from r->uri = /friends/example
我看不出我的规则有什么问题。 / user / example的重写规则正如它应该的那样工作。虽然/ friends /具有相同的规则,但它给了我上述错误。
答案 0 :(得分:1)
解决。我感动了:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^user\/([a-zA-Z0-9_\-\.]+)/?$ /profile.php?username=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^friends\/([a-zA-Z0-9_\-\.]+)/?$ /friends.php?username=$1 [L]
上面:
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
似乎在循环中添加了.php。现在所有规则都完美无缺。