1

I'm trying to rewrite a place name from a url path into a query string on nginx.

I want ourdomain.com/hotels/london?some_key=value to become ourdomain.com?d=london&some_key=value

We were doing it with Apache -

RewriteRule ^hotels/([^/]+)/?\??(.*)$ ?d=$1&$2 [QSA]

And we're currently doing on haProxy (acting as a reverse proxy) -

reqrep ^([^\ :]*)\ /hotels/([^/\ \?]+)/?\??([^\ ]*)(.*)$     \1\ /?d=\2&\3\4

How do I do the same thing on nginx?

1 Answer 1

6

Try something like:

rewrite ^/hotels/([^/]+)/?$ /?d=$1 permanent;

The nginx URI always has a leading /. The ? and query string is not part of the normalised URI, but the rewrite directive appends any arguments automatically unless there is a trailing ?.

See this document for details.

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

Comments

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.