Nginx重写规则

时间:2016-10-04 07:46:49

标签: regex nginx

我有以下网址:

  

mywebsite.com/template/product.html

我希望将其重写为

  

mywebsite.com/product

location / {
  alias /var/www/web/;
  rewrite ^/(mywebsite.com/template/.*)\.html$ /$1 last;
}

1 个答案:

答案 0 :(得分:0)

不确定是否要将域作为通用变量处理。

无论如何,如果你为mywebsite.com设置服务器配置,这个配置应该适合你的情况。

server {
    error_log  logs/mywebsite.com.log  debug;

    server_name mywebsite.com;
    root /var/www/web/mywebsite.com;
    # this directive handle the redirects from old name.html to new format
    location /template/ {
       rewrite ^/template/([^.]+)\.html$ /$1 redirect;
    }
    # given new new url format this directive match the name and
    # read the file  
    location ~* ^/([^.]+)$ {
       try_files /template/$1.html /template/notfound.html;
    }
}

您编写的正则表达式与产品名称不匹配。现在该小组应该符合您的要求。我还修改了last中的重写标记redirect,因为我认为您希望将浏览器或机器人重定向到新网址。