1

我无法找到这个问题的答案,所以请让我知道之前是否已解决。

我正在使用 mod_rewrite 来做“漂亮”的 URL,但是如果你请求一个不存在的文件(比如一个错字),它会重定向并添加 .php 很多次然后失败。我在下面的代码:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://inquisito.rs/$1/ [R=301,l]
RewriteRule ^(.*)/$ /$1.php [L]

因此,如果您访问http://inquisito.rs/aion/,它会向您显示 aion 页面,但如果您意外访问 inquisito.rs/aio/,它会显示此信息

http://inquisito.rs/aio.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php/

在此先感谢您,我无法告诉您有多少次使用此处的信息来解决工作和家庭中的问题。

4

1 回答 1

0

使用您给出的示例,这是应用规则的方式:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f        # /aio/ is not a file, so this matched
RewriteCond %{REQUEST_URI} !(.*)/$         # This DOES NOT match, because you have a trailing slash
RewriteRule ^(.*)$ http://inquisito.rs/$1/ [R=301,L]  # This rule doesn't run, because the condition above wasn't met

# This rule is separate from the RewriteConds above
RewriteRule ^(.*)/$ /$1.php [L]            # This does match because of the lack of RewriteConds and because you have a trailing slash

试试这组(未经测试的)规则:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f      # Make sure no matching file exists
RewriteCond %{REQUEST_URI} !\.php$         # Don't match requests that already end .php
RewriteCond %{REQUEST_URI} !(.*)/$       # Check for missing trailing slash
RewriteRule ^(.*)$ http://inquisito.rs/$1/ [R=301,L]  # Redirect with trailing slash

# Separate rule
RewriteCond %{REQUEST_URI} !\.php$       # Don't match requests that already end .php
RewriteRule ^(.*)/$ /$1.php [L]          # Internal redirect to matching PHP file

需要注意的是,所有匹配的 RewriteRules 都会导致新的请求再次被 htaccess 处理。

于 2013-07-09T10:37:16.443 回答