【问题标题】:apache - redirect to https://wwwapache - 重定向到 https://www
【发布时间】:2018-11-16 04:41:23
【问题描述】:

我遇到了一些麻烦。基本上我所追求的是:

  1. http://example.com -> https://www.example.com
  2. https://example.com -> https://www.example.com

基本上所有的 URL 都应该以 https://www 开头。

我从 LetsEncrypt 获得了 example.com 和 www.example.com 的 SSL 证书。

LetsEncrypt 添加了以下内容(在我的端口 80 vHost 中):

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

但是,这只会将http 重定向到https。它不会将www 添加到任何non-www。我尝试将其复制到我的端口 443 vHost 但导致重定向循环。

有谁知道如何做到这一点?理想情况下,我更愿意在 vHosts 中完成这一切,但不介意是否只能在 htaccess 中完成。

【问题讨论】:

    标签: apache .htaccess vhosts


    【解决方案1】:

    我之前遇到过这个问题,我是这样解决的: 你把它放在 exemple.com.conf 上:

    ServerName exemple.com
    ServerAlias www.exemple.com
    DocumentRoot /var/www/laravel/public/
    Redirect permanent / https://www.exemple.com/
    

    然后:

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.exemple.com [OR]
    RewriteCond %{SERVER_NAME} =exemple.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    

    然后我把它放在我的 exemple.com-le-ssl.conf

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
    RewriteRule (.*) http%1://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
    

    希望对你有帮助:)

    【讨论】:

      【解决方案2】:

      我想我已经找到了解决办法:

      您需要在端口 80 vHost 中保留 LetsEncrypt 添加的现有重定向条件。然后将以下内容添加到端口 443 vHost:

      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
      RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
      

      所以,让端口 80 vHost 重定向到 https,然后在端口 443 vHost 中,处理 non-wwwwww 的重定向。

      【讨论】:

        【解决方案3】:
        <IfModule mod_rewrite.c>
            <IfModule mod_negotiation.c>
                Options -MultiViews
            </IfModule>
        
            RewriteEngine On
        
            # Start with www 
            RewriteCond %{HTTP_HOST} !^www\. [NC]
            RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
            RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
        
        
            # Redirect Trailing Slashes If Not A Folder...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)/$ /$1 [L,R=301]
        
        
            # Handle Front Controller...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        
            # Handle Authorization Header
            RewriteCond %{HTTP:Authorization} .
            RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        </IfModule>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-14
          • 2017-02-02
          • 2020-12-23
          • 2014-12-13
          • 1970-01-01
          相关资源
          最近更新 更多