2

I have this NGINX configuration:

root    /var/www/web;
index   index.php;

server_name  domain.com;
access_log  off;
error_log on;

location / {
    rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}

Now, I want to redirect url something like "domain.com/index.php?tag=1&section=2&type=3" to "domain.com/tag/section/type"

how can I do that, where should I put the code? please help,

Thankyou

I already tried:

location / {
   rewrite ^/index\.php?tag=(.*)&section=(.*)&type=(.*)$ /$1/$2/$3 permanent;
   rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}

but it didnt work..

2
  • See this answer Commented Jan 23, 2019 at 14:44
  • what about the variables? what should I write for the redirect? I tried but still not workin.. Commented Jan 23, 2019 at 15:40

1 Answer 1

7

The rewrite and location directives use a normalized URI which does not include the query string.

To test the query string, you will need to consult the $request_uri or $args variable using an if statement and/or map directive.

The advantage of using $request_uri is that it contains the original request and will help to avoid a redirection loop.

If you only have one redirection to perform, the map solution is probably overfill.

Try:

if ($request_uri ~ ^/index\.php\?tag=(.*)&section=(.*)&type=(.*)$) {
    return 301 /$1/$2/$3;
}
location / {
    ...
}

See this caution on the use of if.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, this is exactly what I am lookin for.. and I just realized should put the code outside the location..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.