How to rewrite path to url with semicolon in nginx? - 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;

Related

Nginx keeps redirecting and adding tailing slashes

I am trying to redirect an app to some static html files.
I am using NGINX as a reverse proxy, and alias to use a specific path.
So I am using a simple Redirect within a react container:
redirectTo="/documentation/index-docs.html"
This is the Nginx config part used for that:
location /documentation {
alias /workspace/documentation;
rewrite ^(.+)/+$ $1 permanent;
The current behavior is that it keeps looping and adding tailing slashes.
http://localhost:3001/documentation/index-docs.html/////////////
Did anyone encounter the same issue before ?
Thanks.

nginx rewrite middle of url to another pattern

This nginx rule works great for me for a full specified file path
rewrite ^/sitemap.xml$ /sitemap.php last;
When I acces sitemap.xml it works as expected but in the background sitemap.php is requested. So far so goode.
Another problem arised and I need to rewrite the last part of existing urls
rewrite ^doctor-solution.html/ doctor-answer.html/ permanent;
What I want to achive is when an old url like
https://example.com/case12232-doctor-solution.html/ is accessed
it must be redirected to
https://example.com/case12232-doctor-answer.html/
But My rule doesn't seem to work. Any ideas?

Rewrite Nginx URLs with Parameters (After a Question Mark)

I have URLS in this format:
/wiki/index.php?title=Widget
/wiki/index.php?title=Blue
/wiki/index.php?title=Red
/wiki/index.php?title=Etc
I want to be able to match any URL that has the pattern "/wiki/index.php?title=" or even just "/wiki/index.php" (but so it will pick up the above URLs with the "?") and then redirect them to simply /wiki/ (all pages that match the above pattern go to the single url /wiki/)
I used to have a Mediawiki install on the /wiki/ directory with a lot of pages in the format above. However now I am running a Wordpress install and it is using the /wiki/ directory. I don't need each rewritten URL to go to a different URL (I know that is difficult as my source URLs have parameters) but right now they all 404 and so I just want to direct to them /wiki/ at least.
Simple URL rewriting
The easiest case (rewrite all /wiki/index.php requests with any arguments) can be done by this config (inside your server block):
location = /wiki/index.php {
rewrite .* /wiki/?;
}
The '?' sign at the end of second rewrite parameter is a trick to completely remove any request arguments. Without it, request /wiki/index.php?title=Widget will be rewrited to /wiki/?title=Widget.
Rewriting only requests matching /wiki/index.php?title=... is a more complex, I don't know how to do it without if construction:
location = /wiki/index.php {
if ($request_uri ~ ^/wiki/index\.php\?title=) {
rewrite .* /wiki/?;
}
}
If you want to generate HTTP 301 or 302 redirect instead of simple URL rewriting, you can use redirect (for 301 temporary redirect) or permanent (for 302 permanent redirect) flag at the end of rewrite directive parameters (see documentation).
Rewriting URLs to individual pages
This task is not as difficult as it seems. For redirecting /wiki/index.php?title=Widget to /wiki/Widget, /wiki/index.php?title=Blue to /wiki/Blue etc. we can make use of map directive:
map $request_uri $uri_suffix {
~^/wiki/index\.php\?title=([^&]*) $1;
}
server {
...
location = /wiki/index.php {
rewrite .* /wiki/$uri_suffix?;
}
...
}
Please note that map directive must be declared outside your server configuration block!

Nginx: rewrite to a file without URL changing

I have tried many options, but did not find a suitable answer. I can do this in Apache, but I can’t figure out how to do a redirect in Nginx while maintaining the URL
.../s1 changes to .../image.jpg - how to fix it?
location /s {
rewrite "/s1" https://example.com/image.jpg last;
}
A redirect always changes the url, a rewrite keeps it internally.
In this case nginx is turning your rewrite into a redirect, because you specified a different server. You have 2 options:
If the url you want to rewrite to is local, you should simply remove the https://example.com part to make the url relative instead of absolute.
If you really want to mask a different server, then what you need to build is a reverse proxy. Nginx does have features for this.

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