.htaccess 如何通过子域将HTTP重新连接到HTTPS?

.htaccess 如何通过子域将HTTP重新连接到HTTPS?,.htaccess,mod-rewrite,url-rewriting,rewrite,.htaccess,Mod Rewrite,Url Rewriting,Rewrite,我有以下HTTP地址: http://www.example.com/ https://secure.example.com/ <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://secure.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule> https://secure.www.ex

我有以下HTTP地址:

http://www.example.com/
https://secure.example.com/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://secure.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
https://secure.www.example.com/
但我的HTTPS地址有一个子域,没有www,如下所示:

http://www.example.com/
https://secure.example.com/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://secure.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
https://secure.www.example.com/

我尝试了以下规则:

http://www.example.com/
https://secure.example.com/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://secure.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
https://secure.www.example.com/

在这种情况下如何强制使用HTTPS?因为我需要删除www


编辑:当我使用WordPress作为CMS时,我在它们上面有这些“奇特URL”的规则:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

重新启动发动机
重写基/
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]

这两条规则都是这样做的:任何非https请求都会被重定向到,同时保留整个url。这应该是您的完整htaccess:

RewriteEngine On
RewriteBase /
#1--Redirect "http://domain to "https://secure"
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://secure.domain.com%{REQUEST_URI} [L,R=301]

#2--wp rules--#
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

它适用于主页,但不适用于其他链接。可能与我在上面设置的规则有关。删除所有规则,只留下我的。试试第一个,如果你不喜欢,试试第二个,但不要一起尝试,他们在做同样的事情。不幸的是,这打破了WordPress的“花哨URL”。你从来没有提到你有WordPress)和其他规则。但这是你应该遵循的逻辑。。它将捕获任何非http请求,并将它们转换为https。下一步该怎么做取决于你的脚本,或者Wordpress,对不起,我直到后来才注意到。我编辑了我的帖子。谢谢,但不幸的是人们不知道子域,因为它只与HTTPS一起使用。你想将主站点重定向到子域吗?是的。我想重定向主站点
http://www.
https://secure.
同时保持WordPress规则适用于奇特的URL(查看OP中的编辑)。提前感谢:)