【问题标题】:htaccess ErrorDocument not always redirectinghtaccess ErrorDocument 并不总是重定向
【发布时间】: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 参数且没有 / 时才会引发错误。

对于任何其他现有页面,aboutcontacts 等,我可以轻松放置或删除 /.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


【解决方案1】:

用这个替换你的页面规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]+)(?:/([^/]*))?/?$ page.php?var1=$1&var2=$2 [L,NC,QSA]

【讨论】:

  • 谢谢,我现在可以写page/var1、page/var1/、page/var1/var2和page/var1/var2/,但是当我尝试在最后添加另一个变量并继续时(page/var1/var2/something),我得到同样的 500 错误。相反,我想要么不执行任何操作并将第三个 / 之后的任何内容视为第二个变量的一部分,要么只是将其视为未找到的页面并让 ErrorDocument 完成其工作。你能帮助我吗 ?无论如何,我感谢你的帮助。
  • 感谢您的尝试,但错误仍然存​​在。 apache日志中的错误是什么意思?我仍然得到那个错误。我搜索了,唯一的解决方案是添加[L]标志,但我已经放了标志
  • 只是为了测试,你可以注释掉ErrorDocument 404这一行,然后再试一次。
  • 好的,因此评论 ErrorDocument 不会更改此特定页面的任何内容。如果我输入一个不存在的页面,我会得到正常的 404 错误,如果我写 page/foo/bar/foobar (只有前两个变量匹配重写规则)我仍然会得到相同的 500 错误。 apache错误日志说同样的,太多的重定向。奇怪的是ErrorDocument被注释了,所以不知道为什么我没有得到正常的404页面
  • 也尝试注释掉“删除斜线”角色。它必须是这些规则之一冲突。您在当前的 .htaccess 中没有任何其他 .htacess 或任何其他规则吗?
猜你喜欢
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
  • 2013-09-13
相关资源
最近更新 更多