I am using nginx in with my Java Application and my problem is that nginx is merging the slashes and I am unable to redirect my website to the correct version.
For instance:
http://goout.cz/cs/koncerty///praha/
is merged to
http://goout.cz/cs/koncerty/praha/
and then I am unable to recognized the malformed URL and perform the redirection.
I tried to set
merge_slashes off;
and then:
rewrite (.*)//(.*) $1/$2 permanent;
But this has no effect and the // stays in the URL.
How can I achieve this?
This works good, but for my setup adding
port_in_redirect off;
was necessary.We encounter the same problem because of mistake, we add two slashes on the URL, and nginx will return 301 error code for the url with two slashes.
The solution for me is:
Add
merge_slashes off;
tonginx.conf
file, and in the location part, addrewrite (.*)//+(.*) $1/$2 break;
The location setting for me is like below:
After add these two lines, when I access my url with two slashes, it will return the result with one slash.
Try this(both nginx only and nginx with openresty configuration) you can improve site SEO by doing these 301 redirection
please keep this code inside the server section for your nginx site conf file
}
its working good for me and 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 redirection so that the SEO of the site will not goes down
Try this (untested):
It might cause multiple redirects if there are multiple groups of slashes though.
Like this:
Might go to:
Then finally: