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


Include GET variables in .htaccess redirect

这是我的.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重写的新手,似乎无法让我的代码以这种方式运行。

谢谢你的帮助。

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

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