Nginx merge_slashes redirect
I am using nginx in my Java application and my problem is that nginx is concatenating forward slashes and I cannot redirect my site to the correct version.
For example:
http://goout.cz/cs/koncerty///praha/
merges with
http://goout.cz/cs/koncerty/praha/
and then I cannot recognize the invalid url and redirect.
I tried to install
merge_slashes off;
and then:
rewrite (.*)//(.*) $1/$2 permanent;
But this has no effect and // is in the url.
How can I achieve this?
Try this one (untested):
merge_slashes off;
rewrite (.*)//+(.*) $1/$2 permanent;
This can cause multiple redirects if there are multiple slash groups.
Like this:
http://goout.cz/////cs/koncerty///praha/
You can go to:
http://goout.cz/cs/koncerty///praha/
Then finally
http://goout.cz/cs/koncerty/praha/
This works well, but an addition was added for my setup port_in_redirect off;
.
We are facing the same issue due to an error, add two slashes to the url and nginx will return a 301 error code for the url with two slashes.
Solution for me:
Add merge_slashes off;
to nginx.conf
file and in the location part addrewrite (.*)//+(.*) $1/$2 break;
Location settings for me are like:
location / {
rewrite (.*)//+(.*) $1/$2 break;
proxy_pass http://http_urltest;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffers 4096 32k;
proxy_buffer_size 32K;
proxy_busy_buffers_size 32k;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
After adding these two lines, when I access my URL with two slashes, it will return a single slash result.
Try this (both nginx only and nginx with open config), you can improve your site SEO by doing this 301 redirect
please save this code in the server section for your nginx config file
server {
........
........
set $test_uri $scheme://$host$request_uri;
if ($test_uri != $scheme://$host$uri$is_args$args) {
rewrite ^ $scheme://$host$uri$is_args$args? permanent;
}
location {
................
................
}
}
its work is good for me and i am using this code now
Example: -
request url- http://www.test.com//test///category/item//value/
result url: - http://www.test.com/test/category/item/value/
301 redirects to prevent website SEO from going down