Apache rewrite rule to redirect a URL with parameters to a URL without parameters

10,006
  RewriteRule (.*) /staticfile.html?

Per the Apache RewriteRule documentation,

By default, the query string is passed through unchanged. * * * * * When you want to erase an existing query string, end the substitution string with just a question mark.

Share:
10,006

Related videos on Youtube

brenguy
Author by

brenguy

Updated on September 18, 2022

Comments

  • brenguy
    brenguy over 1 year

    I want to go from:

     www.website.com/directory/results?category=10&dir=asc
    

    to

     www.website.com/staticfile.html
    

    I tried the following and it doesn't seem to catch:

     RewriteRule "^directory\/results\?category=10&dir=asc" "\/staticfile\.html" [R, L]
    

    I can get it to redirect using:

     RewriteCond %{REQUEST_URI} "^directory\/results" [NC]
     RewriteCond %{QUERY_STRING] category=10&dir=asc
     RewriteRule (.*) "\/staticfile\.html"
    

    But then it appends the parameters after html in the last example.

    • Colt
      Colt almost 8 years
      Interestingly, this question is not actually answered in the canonical question/answer
  • brenguy
    brenguy almost 8 years
    Yep found out that Apache 2.2 does not make use of the QSD flag. In Apache 2.4 this flag is available to shut off the default Apache rewrite behavior of automatically appending query parameters from the old to the new URL. My configuration looks like this and it works: RewriteCond %{QUERY_STRING} category=10&dir=asc&order=name&format=165 RewriteRule (.*) "\/4k-ultra-hd\.html?" [R,L]
  • Colt
    Colt almost 8 years
    @brenguy - BTW, why are you trying to use regular expression syntax in the Substition string? The RewriteRule substition string is plain text, exept for inclusion of back-references, server-variables, and mapping-function calls, all of which have a speficied(not regular expression) syntax
  • brenguy
    brenguy almost 8 years
    If i am understanding the question correctly... are you asking me why I am specifying rewriteconditions or why am I using (.*)?
  • Colt
    Colt almost 8 years
    @brenguy - I am asking about the formating you use for the substitution string (the "\/4k-ultra-hd\.html?" part). It looks like you are writing this as if it was regular expression, which it is not. See the Apache RewriteRule documentation for what may be included in the substitution string. It should probably be just /4k-ultra-hd.html? (i.e., no escape, quotes) as suggested in my answer.