【发布时间】:2013-07-14 06:48:59
【问题描述】:
只是一个小问题真的让我很生气。在服务器上启用 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]
【问题讨论】:
-
在哪里将
IS_HTTP设置为 1,因为该部分已在上面的代码中进行了注释? -
有 2 个重写条件来检查 https,但实际设置在规则中,它本身带有硬编码的 http 或 https,一般来说,它只是最终重定向到 https 部分的子域.还有一个问题是这样做不是做特定于页面的结转,但我可以稍后处理它,因为这是主机 cond 正则表达式的问题。我在想我需要另一个 https on block 来为 https 传入的子域进行重定向
-
是否仅在您的 Intranet 上发生?因为你不能有一个地址像
http://domain这样的互联网域,我的意思是你必须至少有一个像http://domain.com这样的点 -
这纯粹是一个占位符,因为我不想提供真实的域。假设它是一个 www.domain.com 和一个 domain.com 主要和子
-
不,我并不热衷于了解您的真实域名。但是,由于您正在处理
^[^.]+\.[^.]+$类型的正则表达式,因此最好用正确的集合来掩盖它。我也相信domain.com或www.domain.com都是主域,而sub.domain.com和www.sub.domain.com应该是子域。如果我的理解不正确,请告诉我,因为我尝试的解决方案将基于此。
标签: .htaccess mod-rewrite ssl https