NGINX: strip querystring in rewrite

8,474

This is the best rule I found and it is working to me:

rewrite ^/content /? permanent;

Meaning, rewrite all requests:

  • starting in the root of the domain (^/)

  • than followed by "content"

  • to the root (index) of the site (/)

  • removing all query strings it might have (?)

  • and show a 301 redirect in the headers.

I guess that's it.

Share:
8,474

Related videos on Youtube

Roger
Author by

Roger

Be the change you want in the world! - M. Gandhi

Updated on September 18, 2022

Comments

  • Roger
    Roger over 1 year

    I'd like to make a 301 rewrite to the sites's index without any querystring. Like this:

    http://example.com/anypage.asp?anyvar=anyvalue

    To:

    http://example.com/

    Here's a real example:

    http://atipico.com.br/conteudo.asp?P_categ=23

    I am trying to follow this: http://wiki.nginx.org/NginxHttpRewriteModule#rewrite

    (The commented are my attempts):

    location ~ /conteudo\.asp(.*)$ {
        #rewrite ^ / permanent;
        #rewrite ^ /? permanent;
        #return 301 /;
        #return 301 /?;
        #if ($args) { return 301 /; }
    }
    

    It always rewrites to http://atipico.com.br/?P_categ=23

    Any ideas?

    • Admin
      Admin over 12 years
      rewrite ^ /? permanent; works fine. Did you clear browser cache or try with another web browser?
    • Admin
      Admin over 5 years
      Yes I had to clear browser cache for updates to have effect, didn't think this would be the case but tried it after reading @quanta comment and fixed my issue
  • Grumpy
    Grumpy over 11 years
    Adding as a reference for OP: wiki.nginx.org/HttpRewriteModule#rewrite It will explain more details about the ? notation which drops the arguments.