0

如果发生 404 not found 错误,我有重定向到 404 自定义页面的基本代码,但这并不总是按预期工作。我还有一个规则来重写page/foo/bar重定向到的URLpage.php?var1=foo&var2=bar

规则如下:

<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/aI write page/a/,错误就会消失。因此,仅当我仅在 URL 中传递一个/参数且没有.

对于任何其他现有页面等,我可以轻松放置或删除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 的其他片段,例如缓存、压缩和其他非重写相关。现在我将文件剥离到这几行。

    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]
4

1 回答 1

1

将您的页面规则替换为:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]+)(?:/([^/]*))?/?$ page.php?var1=$1&var2=$2 [L,NC,QSA]
于 2013-11-04T18:46:38.117 回答