[英]Incomplete RewriteRules in Apache
我遵守.htaccess
规则
Options +FollowSymlinks
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^captcha/(.*)$ captcha/captcha.php?cid=$1 [QSA,L]
RewriteRule ^index\.(html|php)$ / [R=301,L]
RewriteCond %{REQUEST_URI} !^/(index\.php|robots\.txt|favicon\.ico|(css|images|js|captcha)/)
RewriteRule ^(.*)$ ?get=$1 [QSA]
它应该内部执行以下操作(在浏览器中不更改用户URL)
/something => /index.php?get=something
/?abc=def => /index.php?abc=def
/something?some=var => /index.php?get=something&some=var
从技术上讲,它可以完成我想要的大多数事情。 但是它应该执行重写操作而不更改浏览器中的用户URL(它应该仅重定向内部)。 但是对于第一个和第三个示例,用户浏览器的重定向如下
/something => /something/?get=something
/something?some=var => /something/?get=something&some=var
但这不是我想要的(不应操纵URL-它必须是内部重写)。 有人可以帮忙吗?
这是臭名昭著的尾部斜杠问题。
实际上,如果访问的URL末尾带有反斜杠,则上面的.htaccess
将可以正常工作。 即yourdomain/something/
但不适用于yourdomain/something
如果没有尾随的斜杠,apache会将其视为文件并查找。 找不到它。 它将尝试通过在末尾添加斜杠来修复自身。
到此时, RewriteCond %{REQUEST_URI} !^/(index\\.php|rob...
已经满足, RewriteRule ^(.*)$ ?get=$1 [QSA]
已经处理过一次。并且该网址已经具有?get=something
。
现在,在Apache添加结尾斜杠并处理.htaccess
它不是内部重定向( 200
)。 所以
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
将失败。 然后RewriteCond %{REQUEST_URI} !^/(index\\.php|rob...
将满足,并且RewriteRule ^(.*)$ ?get=$1 [QSA]
将再次得到处理。
并且,apache会在添加结尾斜杠后不久为临时重定向设置URL。 此时,您的URL看起来像yourdomian.com/something/?get=something
。这就是为什么URI会反映在浏览器中的原因。 即
/something => /something/?get=something
/something?some=var => /something/?get=something&some=var
但您的index.php
实际上会看到:
/something => ?get=something/&get=something
/something?some=var => ?get=something/&get=something&some=var
RewriteEngine on
RewriteBase /
DirectoryIndex index.php
#add these 2 lines
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
#add these
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R,L]
RewriteRule ^captcha/(.*)$ captcha/captcha.php?cid=$1 [QSA,L]
RewriteRule ^index\.(html|php)$ / [R=301,L]
RewriteCond %{REQUEST_URI} !^/(index\.php|robots\.txt|favicon\.ico|(css|images|js|captcha)/) [NC]
RewriteRule ^(.*)$ ?get=$1/ [L,QSA]
检查文件是否存在。
RewriteCond %{REQUEST_URI} !-f [OR]
检查请求URI的末尾没有扩展名
RewriteCond %{REQUEST_URI} !\\.\\w+$
检查最后是否有斜杠。
RewriteCond %{REQUEST_URI} !/$
如果满足以上条件,请使用斜杠重定向当前URL。
RewriteRule (.*) /$1/ [R,L]
默认情况下, R
(重定向)具有302
(临时重定向)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.