2016-10-04 124 views
0

我有以下网址:Nginx的重写规则

mywebsite.com/template/product.html

,我希望把它改写为

mywebsite.com/产品

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

不确定我从http请求方面得到了您的问题的用例。你是否想要所有请求'http:// mywebsite.com/template/product.html'必须重定向(302或301)到'http:// mywebsite.com/product'?而且,为什么你在问题标签中添加了apache? – freedev

+0

我以为大多数apache用户都知道nginx。是的,我希望所有请求都被重定向到''http:// mywebsite.com/product /'' –

+0

您是否想将域名作为一个通用变量来处理?这是一种巨大的托管吗? – freedev

回答

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

您写的正则表达式与产品名称不匹配。现在这个小组应该适合你的要求。我还修改了redirect中的重写标记last,因为我想你要将浏览器或机器人重定向到新的URL。

+0

它的工作,但它显示404没有找到。这里有什么问题?不,不是大量托管。我想要在'http:// mywebsite.com/product /''中看到同样的页面,就像在http:// mywebsite.com/template/product.html''中一样。 –

+0

这个问题,是因为这个问题是如何将请求从'/ template/name.html'重定向到'/ name'。没有关于如何以新的url格式读取html的问题。我想你认为新问题的解决方案是预料之中的。无论如何,我已经更新了我的答案。 – freedev