How do I preserve the existing query string in a mod_rewrite rule

后端 未结 2 759
北海茫月
北海茫月 2020-12-11 07:40

I\'m trying to rewrite an url from:

http://domain.com/aa/whatever/whatever.php
to
http://domain.com/whatever/whatever.php?language=aa

However, de

相关标签:
2条回答
  • 2020-12-11 08:09

    You could setup the URL rewrite to pass the language to the php script via the PATH_INFO element of the $_SERVER superglobal. Just pass the language to the script like so:

    foobar.php/en?args
    

    In this case, $_SERVER[PATH_INFO] would equal /en

    0 讨论(0)
  • 2020-12-11 08:20

    From the documentation for mod_rewrite the pattern in RewriteRule matches against the part of the URL after the hostname and port, and before the query string so the query string is not included. That is why you don't get the other variables.

    To add a new query string parameter language=xx whilst preserving any existing query string you need to use the QSA flag (query string append). With this flag, just one rule based on your second case should be sufficient:

    RewriteRule ^([a-z]{2})/(.*) /$2?language=$1 [QSA]
    
    0 讨论(0)
提交回复
热议问题