I am redirecting /example.php?var=value to /index.htm like this:

rewrite /example.php?var=value / permanent;

However, the browser ins printing the final uri like this:


How can I have a clean uri like this?




Excerpt from nginx rewrite module documentation:

If in the line of replacement arguments are indicated, then the rest of the request arguments are appended to them. To avoid having them appended, place a question mark as the last character:

rewrite  ^/users/(.*)$  /show?user=$1?  last;
  • Thanks, @AlexD! So in my case how could I redirect domain.com/example.php?var=anyvalue to /index.htm? I am trying rewrite /example.php?(.*) /? last; or rewrite /example.php?(.*) /? permanent; but it is not working, it continues redirecting to /index.htm?var=anyvalue
    – Roger
    Oct 18 '11 at 15:40

Put a question mask at the end of replacement:

    location ~ \.php$ {
        location ~ /example\.php$ {
            if ($args ~ var=value) {
                rewrite ^ /? permanent;
        include fastcgi.conf;

        fastcgi_intercept_errors        on;
        error_page 404 /error/404.php;
  • Thanks, @quanta! Shouldn't there be any other way to do this without using "if"?
    – Roger
    Oct 18 '11 at 15:41
  • No need to worry. Only rewrite in if condition is 100% safe.
    – quanta
    Oct 18 '11 at 15:49

The simpliest way I found is this:

rewrite ^/example.php /? permanent;

Thanks for all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.