Redirect with nginx (remove substring from url)

半世苍凉 提交于 2021-01-27 19:11:25

问题


I want do a redirect from old url:

http://example.org/xxxxxxxxx.html

To new urls (remove ".html")

http://example.org/xxxxxxxxx

How I can do this with nginx?

EDIT:

xxxxxxxxx can be differ, example:

http://example.org/url-1.html redirect to http://example.org/url-1 http://example.org/another-url.html redirect to http://example.org/another-url


回答1:


location ~ ^(.*)\.html$ {
    return 301 $1;
}



回答2:


Probably you need a rewrite statement

location /xxx.html {
   rewrite ^/xxx(.*) http://example.org/xxxxx permanent;
 }

You detailed explanation please refer https://www.nginx.com/blog/creating-nginx-rewrite-rules/

Another method would be return directive

server {
    listen 80;
    listen 443 ssl;
    server_name www.old-name.com old-name.com;
    return 301 $scheme://www.new-name.com;
}



回答3:


server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.org www.example.org;
    return 301 http://$server_name$request_uri;
}


来源:https://stackoverflow.com/questions/38171713/redirect-with-nginx-remove-substring-from-url

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