V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Totato5749
V2EX  ›  Apache

求教一个 htaccess 写法

  •  
  •   Totato5749 · Feb 28, 2017 · 2449 views
    This topic created in 3350 days ago, the information mentioned may be changed or developed.

    想要实现的需求:

    1. http://www.example.com
    2. http://example.com
    3. https://www.example.com
    4. https://example.com 以上四个链接均跳转到 https://example.com ( http 与 https 区别,有无 www 区别)

    求大神指点

    18 replies    2017-03-03 23:09:28 +08:00
    lslqtz
        1
    lslqtz  
       Feb 28, 2017 via iPhone
    https://example.com 以上四个链接均跳转到 https://example.com
    无限重定向大法
    nfroot
        2
    nfroot  
       Mar 1, 2017
    @lslqtz 服务器要搭配个防火墙来防止 CC 攻击,哈哈哈!
    dailiip
        3
    dailiip  
       Mar 1, 2017   ❤️ 1
    RewriteCond %{SERVER_PORT} !^443$ [OR]
    RewriteCond %{HTTP_HOST} !^example.com$ [NC]
    RewriteRule "^/?(.*)" "https://example.com/$1" [L,R=301]
    Totato5749
        4
    Totato5749  
    OP
       Mar 2, 2017
    @lslqtz
    @nfroot

    不好意思表达的不清楚,我想实现的是前 3 个都跳转到最后一个


    @hqfzone 你这个出个无限重定向了= =
    lslqtz
        5
    lslqtz  
       Mar 2, 2017
    @hqfzone
    @Totato5749
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{HTTP_HOST} !^example.com$ [NC]
    RewriteRule "^/?(.*)" "https://example.com/$1" [L,R=301]
    这个呢?
    lslqtz
        6
    lslqtz  
       Mar 2, 2017
    抱歉,我也写错了。。
    lslqtz
        7
    lslqtz  
       Mar 2, 2017
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{HTTP_HOST} !^example.com$ [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    好吧 刚刚那个似乎没问题
    lslqtz
        8
    lslqtz  
       Mar 2, 2017
    是我写错了。。
    改成这样吧:
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    RewriteCond %{HTTP_HOST} !^example.com$ [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    dailiip
        9
    dailiip  
       Mar 3, 2017
    @lslqtz 你最后那个跟我的有什么区别?我发的是我自己用着的规则
    Totato5749
        10
    Totato5749  
    OP
       Mar 3, 2017
    @lslqtz
    @hqfzone

    不知道为什么你们的规则都会无限重定向,是因为我 dns 设置有问题吗?
    dailiip
        11
    dailiip  
       Mar 3, 2017
    @Totato5749 具体情况不了解,问题解决了就行吧……
    Totato5749
        12
    Totato5749  
    OP
       Mar 3, 2017
    @hqfzone 问题没解决= = 因为会无限重定向,网站进不去
    lslqtz
        13
    lslqtz  
       Mar 3, 2017
    @hqfzone 没区别,但是有可能他的 apache 。。
    lslqtz
        14
    lslqtz  
       Mar 3, 2017
    @Totato5749 其他的 htaccess 呢
    Totato5749
        15
    Totato5749  
    OP
       Mar 3, 2017
    @lslqtz

    我现在的 htaccess 是网上找的,不过是全部跳转到带 www 的 https 地址,我是不想要 www 来着。。

    RewriteEngine on

    RewriteCond %{HTTP:KERSSL} !on
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteCond %{HTTP_USER_AGENT} !MSIE/[1-8]\. [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301]

    RewriteCond %{HTTP:KERSSL} on
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteCond %{HTTP_USER_AGENT} !MSIE/[1-8]\. [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301]

    RewriteCond %{HTTP:KERSSL} !on
    RewriteCond %{HTTP_HOST} ^www.example.com [NC]
    RewriteCond %{HTTP_USER_AGENT} !MSIE/[1-8]\. [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
    lslqtz
        16
    lslqtz  
       Mar 3, 2017
    @Totato5749 那你不会之前的没删就加上去了吧。。
    你自己不会改吗?
    RewriteEngine on

    RewriteCond %{HTTP:KERSSL} !on
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    RewriteCond %{HTTP:KERSSL} on
    RewriteCond %{HTTP_HOST} !^example.com [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    Totato5749
        17
    Totato5749  
    OP
       Mar 3, 2017
    @lslqtz
    @hqfzone

    真的谢谢你们,在 @hqfzone 的基础上改了下可以了,大概是因为 apache 不能用 443 来判断?

    RewriteEngine on

    RewriteCond %{HTTP:KERSSL} !on [OR]
    RewriteCond %{HTTP_HOST} !^example.com$ [NC]
    RewriteRule "^/?(.*)" "https://example.com/$1" [L,R=301]
    lslqtz
        18
    lslqtz  
       Mar 3, 2017
    @hqfzone 这问题
    很怪.....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2376 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 103ms · UTC 01:45 · PVG 09:45 · LAX 18:45 · JFK 21:45
    ♥ Do have faith in what you're doing.