.htaccess、mod_重写和SSL子域重定向

.htaccess、mod_重写和SSL子域重定向,.htaccess,mod-rewrite,ssl,https,.htaccess,Mod Rewrite,Ssl,Https,只是一个小问题,真让我头疼。在服务器上启用了SSL后,我设置了一个.htaccess to do域(保留任何选中的查询字符串/页面)访问重定向以将所有流量发送到https 这主要是有一个例外 http://www.domain > https://www.domain (Works) https://www.domain (No redirect works) http://domain > http://www.domain

只是一个小问题,真让我头疼。在服务器上启用了SSL后,我设置了一个.htaccess to do域(保留任何选中的查询字符串/页面)访问重定向以将所有流量发送到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]
 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目前不处理查询字符串重定向器,但我现在将重点放在子域问题上

以前的.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]
 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]
好的,请尝试以下代码:

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]

如果将
设置为1,因为在上面的代码中对该部分进行了注释?有两个重写条件检查https,但实际设置是在规则中,它自己使用硬编码的HTTP或https。通常情况下,它只是最终重定向到https部分的子域。还有一个问题,就是这样做不是做特定于页面的结转,但我可以稍后再处理,因为这是主机cond regex的问题。我想我需要另一个https-on-block来重定向https上的子域。它是否仅在您的内部网上发生?因为您不能拥有地址为
http://domain
我的意思是你必须至少有一个像
http://domain.com
作为示例,这只是一个占位符,因为我不想提供真正的域。假设它是一个www.domain.com和一个domain.com主域和子域。我不想知道你真正的域。但是,由于您正在处理
^[^.]+\.[^.]+$
这类正则表达式,因此最好使用正确的集合来屏蔽它。另外,我认为
domain.com
www.domain.com
都是主域名,而
sub.domain.com
www.sub.domain.com
应该是子域名。如果我的理解不正确,请告诉我,因为我尝试的解决方案将基于此。好的,我将在一分钟内进行测试,当我有机会时,我将无法使用上面的查询字符串cond(这就是为什么我在最初的帖子中对其进行了注释)因为它是一个内容管理系统,所以p=bit是可变的,即:不可能列出所有可能的页面名称确定您的解决方案99%都是这样。唯一不起作用的是,如果你去(没有www.sub),它不会重定向到www.sub域,相反,firefox会显示可信证书错误/警告等,因为该域上的证书不适用于主域,而只适用于子域。你能在问题中发布完整的最新.htaccess吗(只需屏蔽您的实际域)。很可能存在一些小问题。好了,我注释掉了p=位的查询字符串cond,因为在使用处理meOk查询字符串的请求uri时,它实际上并不需要。这就是问题:
RewriteRule^-[E=is\u HTTP:1]
因为这是无条件的,所以is\U HTTP总是设置为1。这意味着只有第一条规则被执行。请理解is\U HTTP是一个条件变量,需要设置一些条件。如果您不需要这些条件,那么我猜您希望所有页面都使用HTTPS,对吗?