我无法找到答案,所以如果之前已经解决了,请告诉我。
我正在使用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/,它会显示你的永恒之页,但是如果你去,比如说inquisito.rs/aio/意外,就会给你这个
在此先感谢,我无法告诉您有多少次我使用此处的信息来解决工作和家中的问题。
答案 0 :(得分: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再次处理新请求。