如何将子域重定向写入htaccess到https

时间:2013-12-30 13:36:00

标签: regex apache .htaccess mod-rewrite

如何为将example.com重定向到www.example.com编写htaccess规则,我们还有多个子域名,例如subdomain.example.cpm,所以我需要将其重定向到https://subdomain.example.com。但是当我编写htaccess规则时,它会循环播放。请发送如何为此写条件。

1 个答案:

答案 0 :(得分:2)

将此代码放入DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

# example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# http to https for subdomains
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]