.htaccess,mod_rewrite和SSL子域重定向

时间:2013-07-16 07:32:39

标签: .htaccess mod-rewrite ssl https

只是一个小问题,真的是我的坚果。在服务器上启用SSL后,我设置了.htaccess来执行域(保留任何查询字符串/页面选择),重定向将所有流量发送到https。

这主要有一个例外

http://www.domain > https://www.domain   (Works)
https://www.domain                       (No redirect works)
http://domain     > http://www.domain    (no sub domain on initial request redirects to sub domain as SSL only covers the www. sub not *.domain)
https://domain    > http://domain        (fails doesn't prepend the www. sub domain if missing)

我很确定这是一件令人眼花缭乱的事情,我无法为我的生活找到它并让我疯狂。

当前.htaccess

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
#RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
#RewriteCond %{QUERY_STRING} ^$
#set env var to 1
#RewriteRule ^ - [E=IS_HTTP:1]


#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

非常感谢任何建议。

我也有处理子页面的内容,所以如果有人转到http://www.domain/index.php?p=about,它会重定向到https://www.domain/index.php?p=about 现在我当前的htaccess目前还没有处理查询字符串重定向器,但我现在关注的是子域问题。

上一页.htaccess

 RewriteEngine on
    RewriteBase /

    #all pages that are supposed to be http redirected if https
    RewriteCond %{HTTPS} on
    RewriteCond %{ENV:IS_HTTP} 1
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

    #all other pages are sent to https if not already so
    RewriteCond %{HTTPS} off
    RewriteCond %{ENV:IS_HTTP} !1
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

到目前为止使用已发布的答案更新了.htaccess

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
#RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

#all other pages are sent to https if not already so with the
#host name set to www.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !=1
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]

1 个答案:

答案 0 :(得分:1)

好的,试试这段代码:

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

#all the pages are sent to https if not already so with the
#host name set to www.domain.com
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]