【问题标题】:NginX rewrite/redirect rule http://www to http://NginX 重写/重定向规则 http://www 到 http://
【发布时间】:2015-06-15 11:52:56
【问题描述】:

我需要一个基本的重定向规则,所以如果有人键入:www.site.com 将被重定向到 site.com。我需要它在浏览器中看起来整洁干净。

我在我的 nginx 服务器块中试过这个:

# trying redirects from http://   www.site.com to http://   site.com
if ($host = "www.site.com") {
rewrite ^ $scheme://site.com$uri permanent;
}

...

location / {
    try_files $uri $uri/ @modx-rewrite /index.php?/$request_uri;
    location ~ .php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }

但由于某种原因,它不起作用,我得到“找不到服务器错误”。 它还在我的地址中添加了 http S。

如果没有这些规则,我的网站可以正常打开并通过 https 运行,并且页面可以正常打开 https://site.com 链接。

谢谢。

注意:我故意在地址的代码片段中添加了几个空格,否则 stackoverflow 不会让我发布它。

【问题讨论】:

    标签: redirect nginx rewrite


    【解决方案1】:

    让它更简单:

    server {
        listen       80;
        server_name  example.com;
        location / { .........; }
    }
    server {
        listen       80;
        server_name  www.example.com;
        location / { return 301 http://example.com$request_uri; }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-23
      • 2019-02-04
      • 2014-05-08
      • 2018-02-02
      • 2016-01-07
      • 2016-05-08
      • 1970-01-01
      • 2013-06-02
      • 2017-09-03
      相关资源
      最近更新 更多