从HTACCESS规则中保存GET参数

时间:2013-10-26 23:09:01

标签: php regex apache .htaccess mod-rewrite

我的CMS(Expression Engine)需要一些.htaccess规则才能重新编写网址中包含index.php的请求。我最近注意到这个正则表达式虽然有点过于强大......并且正在搞乱这样的GET请求:

domain.com/something/?specialurl=http%3A%2F%2Fsamplesite.com%2Findex.php

此处,index.php位于GET参数中,并导致重定向循环。

我需要一个很好的策略来允许我的htaccess规则继续为我的CMS运行,但是当它们成为查询参数的一部分时,避免弄乱它们。我的正则表达式如下......任何想法都表示赞赏......下面是我目前的.htaccess规则:

# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

提前致谢

3 个答案:

答案 0 :(得分:1)

将条件更改为

RewriteCond %{THE_REQUEST} ^GET[^?]+index\.php [NC]

以上内容适用于您网站上任何位置的所有index.php个文件。如果您只想重定向特定文件,例如/index.php,则可以使用:

RewriteCond %{THE_REQUEST} ^GET\ /index\.php [NC]

答案 1 :(得分:1)

%{THE_REQUEST}包含方法旁边的整个请求行和HTTP协议版本URI的绝对路径 - 但未标准化。

这意味着,如果index.php被网址编码为%69ndex.php,那么您的代码现在已经失败了。

这里的悲伤消息是Apache HTTP在这里没有帮助你。更好地检查PHP脚本中的这些条件并触发并在那里进行适当的重定向。我还说这是正确的地方,因为你应该在那里进行URL规范化。

答案 2 :(得分:1)

我相信你的规则需要NE (NoEscape) flag

请尝试以下规则:

# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule ^(.*?)index\.php/*(.*)$ /$1$2 [R=301,L,NC,NE]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,NE]

PS:如果您已经从某处获得编码请求,则此标志无效。