RewriteRule that preserves GET parameters

13,172

The regex on the RewriteRule is only run against the path part of the URL, not the query parameters. Fortunately there is the [QSA] flag to preserve existing query parameters.

Share:
13,172
tirithen
Author by

tirithen

System developer with focus on JavaScript (mostly excited about Polymer 3) and Go. Always eager to learn new things.

Updated on July 24, 2022

Comments

  • tirithen
    tirithen almost 2 years

    What is wrong with this rewrite rule?

    RewriteRule ^api/(.+)$ api/index.php?url=$1 [L]
    

    I simply want "index.php?url=" to be added after api/ and before the rest of the get parameters.

    api/image/upload&arg1=1&text=lorem+ipsum
    

    to

    api/index.php?url=image/upload&arg1=1&text=lorem+ipsum
    

    What is wrong with (.+) to get everything after api/?

  • tirithen
    tirithen almost 14 years
    I had [L] before but it does not make any diffrence. My problem is that apache2 reports that the only get parameter is [url] => index.php , all other parameters are gone and url parameter should be 'image/upload'. When I use the rule ^api/([a-zA-Z0-9/_]+)$ instead the url parameter is 'image/upload' but I loose all other GET parameters instead.
  • tirithen
    tirithen almost 14 years
    I'm allso not trying to do a redirect, just get the text between api/ and before the first & to get the string prefixed by "index.php?url=" and the rest of the GET parameters preserved
  • tirithen
    tirithen almost 14 years
    Wonderful! This solved my problems! I have allso found a JS function to url encode all non a-zA-Z0-9_. characters cass-hacks.com/articles/code/js_url_encode_decode
  • Thomas Ahle
    Thomas Ahle over 12 years
    How does R=301 work here? It is not in the docs at httpd.apache.org/docs/2.0/mod/mod_rewrite.html
  • SirDarius
    SirDarius over 12 years
    Search for "'redirect|R [=code]' (force redirect)" on that page. It's if you want to specify the exact HTTP response code for the redirect.
  • oyvindhauge
    oyvindhauge almost 7 years
    QSA saved me! Thank you, sir!