2

只是一个小问题真的让我很生气。在服务器上启用 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]
4

1 回答 1

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]
于 2013-07-16T16:51:35.450 回答