• 请不要在回答技术问题时复制粘贴 AI 生成的内容
yantianqi
V2EX  ›  程序员

nginx 下 https 下的 www 跳转不带 www

  •  
  •   yantianqi · Dec 11, 2018 · 4333 views
    This topic created in 2883 days ago, the information mentioned may be changed or developed.
    14 replies  •  2018-12-12 08:59:43 +08:00
    freshmanc
    freshmanc
    Dec 11, 2018
    1
    CNAME 就能干这事了吧
    xiwangzishi
    xiwangzishi
    Dec 11, 2018
    2
    ```nginx
    server{
    listen 443;
    server_name www.abc.com;
    return 301 https://abc.com$request_uri;
    }
    ```
    zpf124
    zpf124
    Dec 11, 2018
    3
    server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    return 301 https://zfly.me$request_uri;
    }


    我是把所有的请求都转到不带 w 的上了, 你如果只需要把 www 转到不带 w 的上,那就把上面的哪个 defalut_server 删了,然后加一行 server_name www.example.com;
    yantianqi
    yantianqi
    OP
    Dec 11, 2018
    4
    @xiwangzishi 对,网上搜索了老半天,突然想到 https 是监听 443,所以配置 443 跳转,是这样配置。
    yantianqi
    yantianqi
    OP
    Dec 11, 2018
    5
    总结了一下配置 nginx 跳转过程[配置 nginx]( https://kkfor.com/article/5c0e8bc5ed10584962b64650)
    xpresslink
    xpresslink
    Dec 11, 2018
    6
    直接在 DNS 上添加一条 CNAME 解析一下多好啊,为毛要这么脱了裤子放屁?
    Anthropic
    Anthropic
    Dec 11, 2018❤️3
    7
    @xpresslink

    1、CNAME 不传递权重
    2、CNAME 只是解析,浏览器地址显示的仍然是原地址,在本例中,如果子域名和裸域名证书不同会报错

    张口就说一件事是脱裤子放屁只能暴露你的无知和素质低下
    xpresslink
    xpresslink
    Dec 11, 2018
    8
    @jarry777 如果需要显示新地址, 现在 DNS 也都支持 301 重定向呀, 绝对比你自己搭的服务器可用性强.
    Timgle168
    Timgle168
    Dec 11, 2018
    9
    配置 nginx 使它强制跳转到 https 就行
    lhx2008
    lhx2008
    Dec 11, 2018 via Android
    10
    @xpresslink dns 301 一般不支持 https
    talas
    talas
    Dec 12, 2018 via iPhone
    11
    RewriteEngine on
    RewriteBase /
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    talas
    talas
    Dec 12, 2018 via iPhone
    12
    呃 apache 下这样实现,找找在线工具可以转 nginx。
    talas
    talas
    Dec 12, 2018 via iPhone
    13
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^name.org [NC]
    RewriteRule ^(.*)$ http://name.org/$1 [L,R=301]
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    这个是不带 www 且 http 跳转 https 的 apache 规则
    lichao
    lichao
    Dec 12, 2018
    14


    仅供参考
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1375 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 65ms · UTC 20:15 · PVG 04:15 · LAX 12:15 · JFK 15:15
    ♥ Do have faith in what you're doing.