简体   繁体   English

如何在NginX中将www.example.com重定向到example.com?

[英]How to redirect www.example.com to example.com in NginX?

When we open example.com (without HTTPS), the site loaded very well but when we open www.example.com (without HTTPS) it opens "Welcome of nginX" but I do not need this, we want to open example.com even if the URL is www.example.com . 当我们打开example.com (不使用HTTPS)时,该网站加载得很好,但是当我们打开www.example.com (不使用HTTPS)时,它会打开“ Welcome of nginX”,但是我不需要这个,我们想打开example.com即使该网址是www.example.com

NOTE: our site has "HTTPS" and when we open example.com , it will be redirected to https://example.com . 注意:我们的网站带有“ HTTPS”,当我们打开example.com ,它将被重定向到https://example.com

NOTE: when we open https://www.example.com , the site loaded well but when the URL is www.example.com or https://www.example.com it was not redirected. 注意:当我们打开https://www.example.com ,该网站加载良好,但是当URL为www.example.comhttps://www.example.com时,未将其重定向。

We are using Ubuntu at AWS. 我们在AWS上使用Ubuntu。

To do this I would recommend editing your nginx configuration file (usually in /etc/nginx/sites-enabled/you-example-config), adding return 301 as seen here , something like: 要做到这一点,我会建议编辑nginx的配置文件(在一般情况在/ etc / nginx的/ /你-例如,配置启用的站点- ),增加收益301所看到这里 ,是这样的:

server {
    listen 80;
    listen 443;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
    #[...ssl settings go here, certs etc....]
}
server {
    listen 443;
    server_name example.com;
    # [...ssl and main settings go here...]
}

This will cause all requests to return https://example.com 这将导致所有请求都返回https://example.com

Have you defined a separate server for each of the options? 您是否为每个选项定义了单独的服务器? See example snippet below. 请参见下面的示例代码段。 Your question looks very similar to this question and this question which both have a lot more information that I won't copy paste here. 您的问题看起来与此问题非常相似, 两个问题都有很多信息,我将不在此处复制粘贴。

This link might also help creating-nginx-rewrite-rules 此链接也可能有助于创建-nginx-rewrite-rules

server {
    listen 80;
    listen 443 ssl;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

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

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

    }

} }

$scheme is the protocol (HTTP or HTTPS) and $request_uri is the full URI including arguments. $scheme是协议(HTTP或HTTPS), $request_uri是包含参数的完整URI。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用.htaccess进行移动重定向,以获取www.example.com或example.com - Mobile redirect with .htaccess for www.example.com OR example.com 将https://example.com重定向到https://www.example.com - Redirect https://example.com to https://www.example.com NGINX https://www.example.com/index.php到https://www.example.com/重定向如何? - NGINX https://www.example.com/index.php to https://www.example.com/ redirect how? how to redirect https://www.example.com to https://example.com with .htaccess? - how to redirect https://www.example.com to https://example.com with .htaccess? 如何从http重定向到https和example.com重定向到www.example.com - How to redirect from http to https and example.com to www.example.com 如何在没有htaccess文件的情况下使https://www.example.com重定向到https://example.com - How to get https://www.example.com to redirect to https://example.com without a htaccess file 如何将https://www.example.com重定向到https://example.com - How to redirect https://www.example.com to https://example.com 如何将https://example.com/重定向到https://www.example.com/ - How to Redirect https://example.com/ to https://www.example.com/ 如何在haproxy中将https://example.com之类的网址重定向到https://www.example.com - how to redirect a url like https://example.com to https://www.example.com in haproxy htaccess重定向https:// www。 (https://www.example.com)到非www(https://example.com) - htaccess redirect https://www. (https://www.example.com) to non www (https://example.com)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM