首页 文章

htaccess重定向到https:// www

提问于
浏览
259

我有以下htaccess代码:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

我希望使用HTTPS将我的站点重定向到 https://www. ,并强制执行 www. 子域,但是当我访问 http://www. (没有HTTPS)时,它不会使用HTTPS将我重定向到 https://www .

12 回答

  • 92

    这适用于https和www

    RewriteCond %{HTTPS} !=on
    # This checks to make sure the connection is not already HTTPS
    RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    
  • 24

    Michals的回答对我有用,虽然有一个小小的修改:

    Problem:

    当你有一个 single site security certificate 时,一个试图访问你的网页的浏览器没有https:// www . (或者您的证书所涵盖的任何域)将显示丑陋的红色警告屏幕,甚至在接收重定向到安全且正确的https页面之前 .

    Solution

    首先使用重定向到www(或证书涵盖的任何域),然后才进行https重定向 . 这将确保您的用户不会遇到任何错误,因为您的浏览器会看到不包含当前网址的证书 .

    #First rewrite any request to the wrong domain to use the correct one (here www.)
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    #Now, rewrite to HTTPS:
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
  • -2

    那里有很多解决方案 . 这是apache wiki的链接,它直接处理这个问题 .

    http://wiki.apache.org/httpd/RewriteHTTPToHTTPS

    RewriteEngine On
    # This will enable the Rewrite capabilities
    
    RewriteCond %{HTTPS} !=on
    # This checks to make sure the connection is not already HTTPS
    
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    # This rule will redirect users from their original location, to the same location but using HTTPS.
    # i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
    # The leading slash is made optional so that this will work either in httpd.conf
    # or .htaccess context
    
  • 25

    要首先强制使用HTTPS,您必须检查正确的环境变量 %{HTTPS} off ,但是上面的规则会在 www. 前面加上因为您有第二条规则来强制执行 www. ,所以不要在第一条规则中使用它 .

    RewriteEngine On
    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.
    # [NC] is a case-insensitive match
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    关于代理

    当某些形式的代理背后,客户端通过HTTPS连接到代理,负载均衡器,乘客应用程序等时, %{HTTPS} 变量可能永远不会 on 并导致重写循环 . 这是因为即使客户端和代理/负载均衡器使用HTTPS,您的应用程序实际上也会接收纯HTTP流量 . 在这些情况下,请检查 X-Forwarded-Proto 标头而不是 %{HTTPS} 变量 . This answer shows the appropriate process

  • 10

    如果您使用的是CloudFlare或类似的CDN,则会在此处提供%解决方案时出现无限循环错误 . 如果您是CloudFlare用户,则需要使用以下内容:

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
  • 0

    糟糕的解决方案和为什么!

    不要使用上面的解决方案,因为当您使用他们的代码时,例如:

    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
    

    浏览器转到:

    http://example.com
    

    然后重定向到:

    https://example.com
    

    然后重定向到:

    https://www.example.com
    

    这对服务器来说太多了 request


    最佳解决方案和答案

    此代码具有 [OR] 条件,以防止URL上的双重更改!

    RewriteEngine on
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
    

    大多数答案甚至接受了一个,不要使用这个技巧 .

  • 541

    在.htaccess文件中设置

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
  • 0

    这是我找到代理用户而不是代理用户的最佳方式

    RewriteEngine On
    
    ### START WWW & HTTPS
    
    # ensure www.
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # ensure https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    ### END WWW & HTTPS
    
  • 0

    如果您使用的是CloudFlare,请确保使用类似的内容 .

    # BEGIN SSL Redirect
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    # END SSL Redirect
    

    这将使您免于重定向循环,并将您的网站安全地重定向到SSL .

    附:如果检查mod_rewrite.c是个好主意!

  • 40
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
    

    注意:确保您已完成以下步骤

    sudo a2enmod重写sudo服务apache2 restart在你的vhost文件中添加以下内容,位于/etc/apache2/sites-available/000-default.conf

    <Directory /var/www/html>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
      Require all granted
    </Directory>
    

    Now your .htaccess will work and your site will redirect to http:// to https://www

  • 133

    要将 http://https:// 重定向到 https://www ,您可以在所有版本的apache上使用以下规则:

    RewriteEngine on
    
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
    

    Apache 2.4

    RewriteEngine on
    
    RewriteCond %{REQUEST_SCHEME} http [OR]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
    

    请注意,自apache 2.4 起,%变量可供使用 .

  • 8

    我尝试第一个答案,它不起作用......这项工作:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{ENV:HTTPS} !=on
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    

相关问题