Write a url path parameter to a query string parameter in nginx - nginx

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?

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.

Related

nginx rewrite preserving CGI query params (with a hash anchor)

For my www.example.com nginx config, I have these rewrite rules:
rewrite ^/foo$ https://one.example.com/page#one permanent;
rewrite ^/foo(\?.*)$ https://two.example.com/page$1#two permanent;
rewrite ^/bar$ https://three.example.com/page#one permanent;
rewrite ^/bar\?(.*)$ https://four.example.com/page?$1#two permanent;
A request for http://www.example.com/foo correctly redirects to https://one.example.com/page#one.
A request for http://www.example.com/bar correctly redirects to https://three.example.com/page#one.
A request for http://www.example.com/foo?extra=yes incorrectly redirects to https://one.example.com/page#one?extra=yes (I expect it to go to https://two.example.com/page?extra=yes#two).
A request for http://www.example.com/bar?extra=yes incorrectly redirects to https://three.example.com/page#one?extra=yes (I expect it to go to https://four.example.com/page?extra=yes#two).
How can I redirect to a page copying CGI parameters and linking to a particular anchor in the destination page?
It appears that the rewrite directive does not handle the # fragment correctly when assembling the query string into the replacement string.
You can prevent rewrite from appending the query string by adding a trailing ? to the replacement string. So, you can construct the correct result using the built-in variables $is_args and $args.
For example:
rewrite ^/foo$ https://one.example.com/page$is_args$args#one? permanent;
See this document for details.
Note that the query string is not part of the normalised URI used to match rewrite and location statements, so your ^/foo(\?.*)$ regular expression will not work.

nginx rewrite all trailing / to /index.html with proxy pass

Using nginx as a reverse proxy, I'd like to mimic the index directive with proxy_pass. Therefore I'd like nginx to query /index.html instead of /, /sub/index.html instead of /sub/.
What would be the best approach to do this ?
Not sure if it's relevant, but the proxied server does answer HTTP 200 on /, but I'd still like to rewrite it to /index.html.
As the / request leaks some information by listing the directory content, I'd also like to be sure that no one will be capable of accessing it (like doing something like /sub/..).
Thanksies
Just add :
rewrite (.*)/$ $1/index.html last;
rewrite (.*)/..$ $1/../index.html last;
Should works

Nginx rewrite rule for query string

I am looking for a Nginx rewrite rule to rewrite (handling relative paths) e.g.
domain.com/en/?var1=val1&var2=val2
to
domain.com/?var1=val1&var2=val2
Which is not simply removing the language code 'en' from the URL, because there are other pages like:
domain.com/en/page1
which should not be rewritten to
domain.com/page1
So only removing when there is a query string in the URL. I have already tried:
rewrite ^/en/?s=(.*) /$args

How to rewrite path to url with semicolon in nginx?

I am using nginx as a reverse proxy, a Angular Universal application is serving in the background.
I am trying to rewrite an old URL pattern to a new one via nginx, namely
server {
rewrite ^/s/(.+)$ /search\;q=$1 permanent;`
...
}
such that something like /s/keywords gets redirected to /search;q=keywords
Unfortunately, the above nginx rule turns
/s/keywords into /search/;q=keywords (so a slash gets added after /search). Is there a chance to remove this slash so the result is /search;q=keywords?
(The application running behind nginx expects the url to be /search;q=keywords.)
You could add quotes to the replacement, for example:
rewrite ^/s/(.+)$ "/search;q=$1" permanent;

NGINX equivalent to Apache mod_rewrite's noescape

I'm trying to migrate a server from Apache to NGINX. It sits in front of an S3 bucket to provide access control and some URL rewriting. I was able to switch everything over except this one weird rewrite rule:
RewriteRule ^([^+]*)\+\+(.*)$ http://s3.amazonaws.com/bucket$1\%2b\%2b$2 [P,NE]
We have a one file that needs to have "++" in the URL. When you request it from S3 the +'s need to be URL escaped. The noescape flag lets you do this in Apache. I tried to do this in NGINX as:
rewrite ^([^+]*)\+\+(.*)$ $1%2b%2b$2 last;
but the percent signs get double escaped and "++" was replaced with "%252b%252b". Is there any way to do this with NGINX?
Try escaping % with another %:
rewrite ^([^+]*)\+\+(.*)$ $1%%2b%%2b$2 last;

Resources