2

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:

domain.com/?var=value

How can I have a clean uri like this?

domain.com

Thanks.

2

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;
1
  • 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
2

Put a question mask at the end of replacement:

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

        fastcgi_intercept_errors        on;
        error_page 404 /error/404.php;
    }
2
  • 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
0

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.