欢迎来到cool的博客
7

Music box

Click to Start

点击头像播放音乐
新博客链接

NginxRedirect.md 配置nginx 跳转到另外一个链接

https://gist.github.com/esfand/8246661

 

HTTP Solution

From the documentation, "the right way is to define a separate server for example.org":

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

server {
    listen       80;
    server_name  www.example.org;
    ...
}

HTTPS Solution

For those who want a solution including https://...

server {
        listen 80;
        server_name www.domain.com;
        // $scheme will get the http protocol
        // and 301 is best practice for tablet, phone, desktop and seo
        return 301 $scheme://domain.com$request_uri;
}

server {
        listen 80;
        server_name domain.com;
        // here goes the rest of your config file
        // example 
        location / {

            rewrite ^/cp/login?$ /cp/login.php last;
            // etc etc...

        }
}

Note: I have not originally included https:// in my solution since we use loadbalancers and our https:// server is a high-traffic SSL payment server: we do not mix https:// and http://.

To check the nginx version, use nginx -v. Here is a solution for older nginx versions...

Strip www from url with nginx redirect

server {
    server_name  www.domain.com;
    rewrite ^(.*) http://domain.com$1 permanent;
}

server {
    server_name  domain.com;
    #The rest of your configuration goes here#
}

So you need to have TWO server codes.

Add the www to the url with nginx redirect

返回列表