【问题标题】:nginx how do I redirect a url to a specific pagenginx如何将url重定向到特定页面
【发布时间】:2015-10-19 22:30:53
【问题描述】:

我看过this question,它描述了如何在nginx中重定向一个url,如下所示

# main server block for www.test.com
server {
    listen       80;
    server_name  www.test.com;
    ...
}

# redirect test.com to www.test.com
server {
        server_name test.com;
        return 301 $scheme://www.test.com$request_uri;
}

我需要做的是重定向一组单独的页面,所以想知道如何做到这一点

例如test.com\index , test.com\home , test.com\maintest.com\index.php

然后我有一些其他页面可以简单地重定向到 .php 扩展名

例如test.com\about\test.com\about.php

例如test.com\contact\test.com\contact.php

最好的方法是什么?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    找到答案...假设test.com 的以下服务器块

    server {
        listen       80;
        server_name  www.test.com;
        ...
    }
    

    添加适当的正则表达式位置路径,并将rewritereturn 添加到重定向网址。

    对于 test.com\indextest.com\hometest.com\maintest.com\index.php

    location ~ ^/(index|home|main) {
        rewrite ^/.* http://$server_name/index.php permanent;
    } 
    

    对于 test.com\about\test.com\about.php

    location /about {
        rewrite ^/.* http://$server_name/about.php permanent;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 2018-11-21
      • 2016-12-26
      • 1970-01-01
      相关资源
      最近更新 更多