Apache 将 www 和非 www 以及子域名重定向到非 www https

Apache 将 www 和非 www 以及子域名重定向到非 www https

我想将 www 和非 www 以及子域名重定向到非 www HTTPS。

我认为这应该澄清。

<VirtualHost *:80>
    DocumentRoot "C:/www/eshop"
    ServerName shop.example.com
    ServerAlias www.shop.example.com
    
    # shop.example.com should redirect to https://shop.example.com
    # www.shop.example.com should redirect to https://shop.example.com
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "C:/www/eshop"
    ServerName shop.example.com
    ServerAlias www.shop.example.com

    # I would like to redirect www and non-www to https://shop.example.com
    
    # https://shop.example.com should redirect to https://shop.example.com
    # https://www.shop.example.com should redirect to https://shop.example.com

    <Directory "C:/www/eshop">
        # some code will go here
    </Directory>
    
</VirtualHost>

答案1

使用Redirect 指令简单重定向

<VirtualHost *:80>
    ServerName shop.example.com
    ServerAlias www.shop.example.com
    Redirect "/" "https://shop.example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.shop.example.com
    Redirect "/" "https://shop.example.com/"
    # ... Maybe some SSL configuration is needed here
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "C:/www/eshop"
    ServerName shop.example.com
    # ... Maybe some SSL configuration is needed here

    <Directory "C:/www/eshop">
        # some code will go here
    </Directory>
    
</VirtualHost>

相关内容