我有以下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://www。使用HTTPS,并强制执行www。子域名, 但当我访问http://www时。(没有HTTPS),它不会重定向到https://www与HTTPS。


当前回答

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

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

Sudo a2enmod重写 Sudo服务apache2重启 在vhost文件中添加Following,该文件位于/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>

现在你的.htaccess将会 Work和您的网站将重定向到http://到https://www

其他回答

有很多解决方案。这里有一个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

要首先强制使用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连接到代理、负载均衡器、Passenger应用程序等,%{HTTPS}变量可能永远不会打开,并导致重写循环。这是因为即使客户端和代理/负载均衡器使用的是HTTPS,应用程序实际上接收的是纯HTTP流量。在这些情况下,检查X-Forwarded-Proto报头,而不是%{HTTPS}变量。这个答案显示了适当的过程

类似于Amir Forsati的解决方案htaccess重定向到https://www,但对于变量域名,我建议:

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

michal的回答对我很有效,尽管做了一个小小的修改:

问题:

当您只有一个站点安全证书时,浏览器试图访问您的页面而没有https:// www。(或证书覆盖的任何域)将显示一个丑陋的红色警告屏幕,甚至在接收到安全正确的HTTPS页面的重定向之前。

解决方案

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

#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]

我试着第一个回答,它不工作… 这项工作:

<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