Nginx replace // with / in URL - nginx

We are currently getting web requests with www.domain.com//index.php which are resolving, but causing issues with google. How can we rewrite the request to catch these and redirect to www.domain.com/index.php
Thanks

By default ngingx merges double-slashes and urls like www.domain.com//index.php works well.
You can turn off merging and make rewrite rule with redirection:
merge_slashes off;
rewrite (.*)//(.*) $1/$2 permanent;

Related

Nginx rewrite rule not works when using proxy_pass

I am using nginx, and is configured to proxy_pass the requests
proxy_pass http://127.0.0.1:3000/;
So when user asks for
https://example.com/salon/1234
every thing works fine.
Now I need this URL to works either
https://example.com/salon/1234/anystringt
So I wrote this
rewrite ^(.*)/salon/([0-9].*)/(.+)$ /salon/$2 last;
and it does hot work.
When I change the rewrite rule to
rewrite ^(.*)/salon/([0-9].*)/(.+)$ /salon/$2 redirect;
everything works fine but it redirecting the user.
Any one knows why the first rewrite rule does not work?

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

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

Split request_uri in nginx

I'm new to Nginx, I'm trying to do proxy_pass for a specific format of URL.
For example "http://example.com/sync/test" is the URL accessed by the user, internally the URL has to do proxy_passs to apache server like, "http://localhost:8000/test" need to exclude sync, but now it's` redirecting has "http://localhost:8000/sync/test".
Is there any option to split and get parameters after sync,
Suggestions or solution is appreciated
Rewrite the url by removing /sync/ from the url
location / {
rewrite ^/sync/(.*) /$1 break;
proxy_pass http://localhost:8000;
}

proxy_pass in nginx to publish webapp under a different directory

I have this location element:
location ~* ^/publicapp {
proxy_pass https://myserver.domain.local;
}
The server myserver.domain.local hosts a web application located under /myapp.
I want to make it publicly available via https://www.mywebsite.com/publicapp. How do I tell nginx to translate /myapp to /publicapp?
Please keep in mind that I use ~* to allow case-insensitivity. Thus, I cannot use a URI with proxy_pass.
Kind regards,
Kevin
Try this:
location ~* /publicapp/ {
rewrite ^/publicapp/(.*)$ /myapp/$1 break;
proxy_pass https://myserver.domain.local;
}
This will rewrite your path and use new one at the .local server.
It works using
rewrite ^/publicapp/(.*) /myapp/$1 break;
At least it does with my very simple application.
Now I have to figure out how to do proper link translation (sorry for using ISA Server/TMG terms, don't know if it's the same in nginx).
Thanks to pythagor :-)
edit:
Works only if I keep a trailing slash after the url in the browser (https://www.mywebsite.com/publicapp/).
another edit:
To make sure URLs end with a slash:
rewrite ^([^.]*[^/])$ $1/ permanent;
Taken from: here (first answer)

Resources