dpzr52746
2014-04-25 23:31
浏览 38
已采纳

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

Here is my .htaccess code:

#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]

I would like to redirect all requests for files name "index.php" in any of the sub directories to the root "index.php" with the path as a get variable. So:

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

This currently works. However, the problem is that if the original url has GET variables in it, they are removed and not included in the path. So:

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

How can this be done? I am new to URL rewriting and can't seem to get my code to behave in this way.

Thanks for your help.

图片转代码服务由CSDN问答提供 功能建议

这是我的.htaccess代码:

  #Rewrite settings  
Options + FollowSymlinks 
RewriteEngine on 
 
#从url 
RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /(.*)index \ .php($ | \ | \ ?,删除index.php  )
RewriteRule ^ /%1 [R = 301,L] 
 
#添加尾部斜杠
RewriteBase / 
RewriteCond%{REQUEST_FILENAME}!-f 
RewriteCond%{REQUEST_URI}!(。*)/ $ 
RewriteRule ^  (。*)$ / $ 1 / [L,R = 301] 
 
#制作整个网址变量
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
s应该成为:
http:// mywebsite。  com / index.php?path = this / is / the / path /%3Fget%3Dvariables 
   
 
 

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

感谢您的帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshi1914 2014-04-26 04:57
    已采纳

    You just need to add query string append (QSA) flag like this:

    RewriteRule ^(.*/)$ /index.php?path=$1 [R=301,QSA]
    
    点赞 打赏 评论

相关推荐