在.htaccess重定向中包含GET变量

时间:2014-04-25 23:31:53

标签: php apache .htaccess mod-rewrite url-rewriting

这是我的.htaccess代码:

#Rewrite settings
Options +FollowSymlinks
RewriteEngine on

#Remove index.php from url
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

#Add trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]

#Make entire url variable
RewriteRule ^(.*/)$ /index.php?path=$1 [R=301]

我想将任何子目录中的文件名“index.php”的所有请求重定向到根“index.php”,路径为get变量。所以:

http://mywebsite.com/this/is/the/path/index.php
becomes:
http://mywebsite.com/index.php?path=this/is/the/path/

目前有效。但问题是,如果原始url中包含GET变量,则会删除它们并且不包含在路径中。所以:

http://mywebsite.com/this/is/the/path/index.php?get=variables
should become:
http://mywebsite.com/index.php?path=this/is/the/path/%3Fget%3Dvariables

如何做到这一点?我是URL重写的新手,似乎无法让我的代码以这种方式运行。

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您只需要添加查询字符串追加(QSA)标记,如下所示:

RewriteRule ^(.*/)$ /index.php?path=$1 [R=301,QSA]