【发布时间】:2013-11-15 10:59:04
【问题描述】:
如果发生 404 not found 错误,我有重定向到 404 自定义页面的基本代码,但这并不总是按预期工作。我还有一条规则来重写将page/foo/bar 重定向到page.php?var1=foo&var2=bar 的URL
规则如下:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^page/(.*)/(.*)$ page.php?var1=$1&var2=$2 [L]
</IfModule>
所以,这很好用,我可以检索这两个变量。但是,如果我在 URL 栏中写入 page/a 会引发错误并显示消息 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request. 该消息说错误是由于 ErrorDocument 规则造成的。如果我键入一个不存在的 URL,ErrorDocument 可以工作,但在这种特殊情况下它不会。
我还想提一下,如果我不写page/a 而写page/a/,错误就会消失。因此,仅当我仅在 URL 中传递 one 参数且没有 / 时才会引发错误。
对于任何其他现有页面,about、contacts 等,我可以轻松放置或删除 / 和 .htaccess 自动将我重定向到正确的页面,而无需 /
这是修改 URL 删除扩展名和 / 的代码
# ------------------------------------------------------------------------------
# | Remove the slash |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]
</IfModule>
# ------------------------------------------------------------------------------
# | Redirect to extensionless url |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
</IfModule>
这是 ErrorDocument 的代码:
ErrorDocument 404 /boilerplate/template/404.php
最后,Apache 错误日志中的消息:
[core:error] [pid 4400:tid 1672] [client ::1:65310] 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.
编辑
这是关于重写的全部部分
# ##############################################################################
# # URL REWRITES #
# ##############################################################################
# ------------------------------------------------------------------------------
# | Rewrite engine |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /boilerplate
</IfModule>
# ------------------------------------------------------------------------------
# | Remove the slash |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]
</IfModule>
# ------------------------------------------------------------------------------
# | Redirect to extensionless url |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
</IfModule>
# ------------------------------------------------------------------------------
# | Pretty urls |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^about/([^/]+)(?:/([^/]*))?/?$ about.php?var1=$1&var2=$2 [L,NC,QSA]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
编辑 2
所以,我删除了 .htaccess 中的所有内容,除了这几条规则。什么也没有变。我清除了 cookie,清空了缓存并清除了所有浏览器历史记录。另外,我用其他浏览器尝试过,同样的事情发生了。为了清楚起见,我没有另一个.htaccess 文件,原始文件包含来自HTML5 Boilerplate 的其他sn-ps,例如缓存、压缩和其他非重写相关。现在我将文件剥离到这几行。
Options +FollowSymlinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /boilerplate
# ------------------------------------------------------------------------------
# | Redirect to extensionless url |
# ------------------------------------------------------------------------------
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# ------------------------------------------------------------------------------
# | Pretty urls |
# ------------------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^page/([^/]+)(?:/([^/]*))?/?$ page.php?var1=$1&var2=$2 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
【问题讨论】:
-
你能提供你的整个htaccess文件吗?
-
嗨,@anubhava 已经回答并部分解决了我的问题,但是这里是 htaccess 的 URL 重写部分。请参阅anubhava 答案下的评论。
-
这些规则是否在名为“样板”的目录中?
-
是的,这只是我用于启动的目录
标签: .htaccess mod-rewrite http-status-code-404 errordocument