Url Nginx合并\斜杠重定向

Url Nginx合并\斜杠重定向,url,redirect,nginx,Url,Redirect,Nginx,我在Java应用程序中使用nginx,我的问题是nginx正在合并斜杠,我无法将我的网站重定向到正确的版本 例如: http://goout.cz/cs/koncerty///praha/ 合并到 http://goout.cz/cs/koncerty/praha/ 然后我无法识别格式错误的URL并执行重定向 我试着设置 merge_slashes off; 然后: rewrite (.*)//(.*) $1/$2 permanent; 但这没有效果,//保留在

我在Java应用程序中使用nginx,我的问题是nginx正在合并斜杠,我无法将我的网站重定向到正确的版本

例如:

   http://goout.cz/cs/koncerty///praha/
合并到

   http://goout.cz/cs/koncerty/praha/
然后我无法识别格式错误的URL并执行重定向

我试着设置

   merge_slashes off;
然后:

    rewrite (.*)//(.*) $1/$2 permanent;
但这没有效果,//保留在URL中

我如何才能做到这一点?

试试这个(未经测试):

但如果有多组斜杠,可能会导致多个重定向

像这样:

http://goout.cz/////cs/koncerty///praha/
可访问:

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

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

这很好,但对于我的设置,添加
port\u in\u redirect off是必需的

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

我的解决方案是:

添加
merge\u斜杠
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时,它将用一个斜杠返回结果。

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

请将此代码保存在nginx站点配置文件的服务器部分中

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

它的工作对我很好,我现在使用这个代码

例如:-

请求url-

结果url:-


301重定向,这样网站的搜索引擎优化就不会下降

“如果”是邪恶的,但在这种情况下,它会起作用,因为重写是一个退出条件。服务器块还避免了一系列位置块效应。“如果是邪恶的”这篇文章也是这么说的。我应该把这个放在文件的什么地方?在一个街区?服务器块?文件的根目录?
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 {
    ................
    ................
}