.htaccess、mod_rewrite 和 SSL 子域重定向

标签 .htaccess mod-rewrite ssl https

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

除了一个异常(exception),这主要有效

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]

最佳答案

好的试试这个代码:

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]

关于.htaccess、mod_rewrite 和 SSL 子域重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17670614/

相关文章:

apache - htaccess 重定向的奇怪行为

javascript - 如何使用 .htaccess 阻止 ip

.htaccess - 如果没有 `www`,域名将无法工作

url - 使用 .htaccess 从 URL 末尾删除查询字符串

node.js - 混淆 SSL 与 Node 和使用 cURL

css - CodeIgniter HTAccess 不加载 CSS 文件

PHP 使用 .htaccess 漂亮的链接 url

php - URL 重写 - $_GET 变量未通过

java - Apache HttpClient 使用自己的 SSL 证书

python - 尝试在 Python 服务器上使用 SSLContext 对象包装套接字时遇到问题