4

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?

4

4 回答 4

6

试试这个(未经测试):

merge_slashes off;
rewrite (.*)//+(.*) $1/$2 permanent;

如果有多组斜杠,它可能会导致多次重定向。

像这样:

http://goout.cz/////cs/koncerty///praha/

可能会去:

http://goout.cz/cs/koncerty///praha/

然后最后:

http://goout.cz/cs/koncerty/praha/
于 2013-02-14T04:29:10.937 回答
1

这很好用,但对于我的设置添加port_in_redirect off;是必要的。

于 2014-02-11T11:13:47.160 回答
0

由于错误我们遇到了同样的问题,我们在URL上添加了两个斜杠,nginx会为带有两个斜杠的url返回301错误代码。

我的解决方案是:

添加merge_slashes off;nginx.conf文件,并在位置部分,添加rewrite (.*)//+(.*) $1/$2 break;

我的位置设置如下:

    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;
    }

添加这两行后,当我使用两个斜杠访问我的 url 时,它将返回一个斜杠的结果。

于 2016-12-18T06:34:57.147 回答
-2

试试这个(仅限 nginx 和带有 openresty 配置的 nginx),您可以通过执行这些 301 重定向来改善站点 SEO

请将此代码保留在您的 nginx 站点 conf 文件的服务器部分中

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 {
    ................
    ................
}

}

它对我有用,现在正在使用此代码

例子:-

请求网址- http://www.test.com//test///category/item//value/

结果网址:- http://www.test.com/test/category/item/value/

301重定向,使网站的SEO不会下降

于 2017-04-14T09:58:53.843 回答