【问题标题】:.htaccess, mod_rewrite and SSL subdomain redirects.htaccess、mod_rewrite 和 SSL 子域重定向
【发布时间】: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.comwww.domain.com 都是主域,而sub.domain.comwww.sub.domain.com 应该是子域。如果我的理解不正确,请告诉我,因为我尝试的解决方案将基于此。

标签: .htaccess mod-rewrite ssl https


【解决方案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]

【讨论】:

  • 好的,我会在一分钟内给它一个测试,当我有机会我不能使用上面的查询字符串 cond 时(这就是为什么它在我的初始帖子中被注释掉了),因为它是一个内容管理系统,所以 p= 位是可变的,即:不可能列出所有潜在的页面名称
  • 好的,您的解决方案已经完成了 99%。唯一不起作用的是如果你去domain.com(没有www.sub)它不会重定向到www。子域,而不是 firefox 显示受信任的证书错误/警告等,因为该域上的证书不适用于主域,它仅适用于子域。
  • 您能否在您的问题中发布您完整和最新的 .htaccess(只需掩盖您的实际域)。那里很可能存在一些小问题。
  • 你去吧,我将 p= 位的查询字符串 cond 注释掉,因为在使用为我处理查询字符串的 request_uri 时实际上不需要它
  • 好的,这就是问题所在:RewriteRule ^ - [E=IS_HTTP:1] 因为这是无条件的,所以 IS_HTTP 总是设置为 1。这意味着只有第一条规则被执行。请理解 IS_HTTP 是一个条件变量,它需要一些条件来设置。如果您不需要这些条件,那么我猜您希望您的所有页面都使用 HTTPS,对吗?
猜你喜欢
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2010-11-12
  • 2015-09-12
  • 1970-01-01
相关资源
最近更新 更多