nginx redirect url to new pattern

给你一囗甜甜゛ 提交于 2019-12-04 06:35:50

问题


I'm currently switching my blog from Wordpress to Ghost. There is nginx in front of ghost. After migration i recognized that old urls

http://domain.org/2015/10/some-topic

are migrated like

http://domain.org/some-topic

So date is gone. Anyway there is some backlinking i don't want to loose, but i'm not so familiar with nginx...So what is the best way to redirect from old url style to new?

My curent configuration looks like:

server {
            listen 80;
            server_name domain.org;

            location / {
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header HOST $http_host;
             proxy_set_header X-NginX-Proxy true;

            proxy_pass http://10.240.0.2:2368;
            proxy_redirect off;
            }
    }

What should be added?. I suppose i need new location but how it should look like?


回答1:


I would recommend using a map:

map $uri $redirect_topic {
    "~^/\d{4}/\d{2}/(?<topic>.*)" $topic;
}

server {
    listen 80;
    server_name domain.org;

    if ($redirect_topic) {
        return 301 $scheme://$host/$redirect_topic;
    }

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://10.240.0.2:2368;
        proxy_redirect off;
    }
}



回答2:


I think you should put into server section:

rewrite ^/[0-9]*/[0-9]*(/.*) $1 last;

But if you've any additional requests maybe would better in a location section (as you wrote).

About more information see on the official nginx documentation.



来源:https://stackoverflow.com/questions/32977170/nginx-redirect-url-to-new-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!