Nginx Redirect when URL includes variable p=1

Posted by ChrisD on Server Fault See other posts from Server Fault or by ChrisD
Published on 2014-08-23T07:04:25Z Indexed on 2014/08/23 10:22 UTC
Read the original article Hit count: 211

Filed under:

Need to write a small nginx rewrite line(s) to alter/301 redirect some URLs within our existing website.

for example:

www.example.com.au/pageone.html?p=1 to www.example.com.au/pageone.html

www.example.com.au/pagetwo.html?dir=asc&limit=200&order=price&p=1 to www.example.com.au/pagetwo.html?dir=asc&limit=200&order=price

www.example.com.au/pagethree.html?dir=dsc&limit=100&order=price&p=1 to www.example.com.au/pagethree.html?dir=dsc&limit=100&order=price

As you can see p=1 has been stripped from the URL's (as it is superfluous but has been live on the site and needs to be redirected now) - all http and https links.

Basically if, and only if, p=1 is used anywhere within the URL then it should redirect to the same URL without the p=1. This should also let p=11, p=12 through as normal (and not redirect), as it is not specifically p=1.

# # # #

If that is not possible then, I'd like to know how to redirect this kind of URL as a standalone one off:

www.example.com.au/pageone.html?p=1 to www.example.com.au/pageone.html

I tried several redirects but they were all pointless and did not work, and was not able to get it working. To be honest I do not really know where to start with this - I am new to nginx.

© Server Fault or respective owner

Related posts about nginx