使用.htaccess强制使用www和https

时间:2013-06-04 18:34:36

标签: .htaccess google-chrome ssl https http-redirect

我们有一个WP Multisite安装,主站点和一个子站点。 我们刚从Godaddy购买了UCC证书,并希望通过https保护整个网站。

我们期待: 将domain.com重定向到www.domain.com 将http://www.domain.com重定向到https://www.domain.com

换句话说,在所有内容上强制使用www和https ..

我尝试对.htaccess文件进行一些修改但没有成功。似乎会导致谷歌浏览器出现问题。

提前感谢您的任何反馈或建议!

S Cranston

这是我当前的.htaccess文件:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
<IfModule mod_headers.c>
   SetEnvIf Origin "http(s)?://(www\.)?(www.simplymusicteachers.com/wp-admin/admin-ajax.php|www.simplymusicteachers.com)$" AccessControlAllowOrigin=$0$1
   Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>

以下是添加了附加代码的文件:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
<IfModule mod_headers.c>
   SetEnvIf Origin "http(s)?://(www\.)?(www.simplymusicteachers.com/wp-admin/admin-ajax.php|www.simplymusicteachers.com)$" AccessControlAllowOrigin=$0$1
   Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## force HTTPS and www. if any of them are not already present
RewriteCond %{HTTP_HOST} (?!^www\.)^(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L]

1 个答案:

答案 0 :(得分:4)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## force HTTPS and www. if any of them are not already present
RewriteCond %{HTTP_HOST} (?!^www\.)^(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L]

验证一切正常后,将R=302替换为R=301。在测试mod_rewrite规则时,请避免使用R=301(永久重定向)。

编辑如果由于某种原因不支持lookbehind尝试:

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.sub.domain.com%{REQUEST_URI} [R=302,L]