apache - htaccess 重定向 http ://and http://www to https://that plays nice with subdomains

标签 apache .htaccess ssl https rackspace-cloud

我正在尝试重定向以下条件:

http://mydomain.com
http://www.mydomain.com

到:

https://mydomain.com

但我不希望它弄乱任何带有子域的东西,这样键入的人:

http://m.mydomain.com

不会被重定向到 https。我目前的 htaccess 文件如下所示:

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

我已经尝试使用我在此处找到的关于堆栈溢出的一些示例,例如这个:

RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

但是,尽管它似乎对那个人有效,但对我来说却导致了“重定向循环”。顺便说一句,在我完成这项工作后,我需要添加一些重定向来处理子域,以无形地(不更改 URL)重定向 http://m.mydomain.comhttp://mydomain.com/m例如。所以无论在这里做什么都不应该阻止这种情况的发生。

如果有帮助,该站点托管在 Rackspace 云站点上

有人知道怎么做吗?

谢谢!

编辑

我试过这个:

#http to https
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [L,R=301]

#subdomain 1
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

它似乎在大多数情况下都有效,除了当我输入一个子文件夹时它似乎不起作用。例如,如果我在地址栏中输入:

mydomain.com/temp

它解析为:

http://mydomain.com/temp/

编辑2

嗯,我已经取得了一些进展。到目前为止,我已经将 www 重定向到非 www,并且子域正常工作(尽管地址栏确实发生了变化 - 我想这是可以接受的)。似乎搞砸了的是,如果我在那里放置任何类型的强制 https。它似乎与 www 到非 www block 冲突。我可以有一个或另一个。有没有办法让这两个部分协同工作?

此外,WordPress 站点地址和 WordPress 地址都设置为 https://mydomain.com

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

# END WordPress

# Redirect from www to non-www location
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

# Redirect http to https
# THIS DOES NOT WORK - causes redirect loop
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

# subdomain
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

RewriteBase /
RewriteCond %{HTTP_HOST} ^sub2.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub2/$1 [R=301,L]

结论

对于大多数网络托管服务商来说,anubhava 的解决方案都可以正常工作。但是,我相信这里的关键是 Rackspace。他们有自己的首选方法,我通过搜索他们的知识库找到了这些方法。这是最终的 htaccess 文件,效果很好。所有 www url 都被发送到非 www url,http 被发送到 https,子域重定向到它们正确的子目录,而不会弄乱 WordPress:

RewriteEngine On
RewriteBase /

#subdomain 1
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

#subdomain 2
RewriteCond %{HTTP_HOST} ^sub2.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub2/$1 [R=301,L]

#get rid of www, works with rackspace cloud sites
RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

#force https, works with rackspace cloud sites
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,L]

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

非常感谢您为此提供的所有帮助。

最佳答案

您可以像这样拥有您的 .htaccess:

RewriteEngine On
RewriteBase /

#subdomain 1
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

#subdomain 2
RewriteCond %{HTTP_HOST} ^sub2.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub2/$1 [R=301,L]

#get rid of www, works with rackspace cloud sites
RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

#force https, works with rackspace cloud sites
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,L]

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

PS:请注意,HTTPS 服务器变量在 Rackspac 云环境中未正确实现。在 Rackspace 云中查看 HTTPS 的正确变量是 ENV:HTTPS .

关于apache - htaccess 重定向 http ://and http://www to https://that plays nice with subdomains,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20988722/

相关文章:

linux - 将培训视频流式传输到远程办公室

apache - IBM HTTP Server v7 - 404 自定义错误页面 - (WAS CE)

windows - FOSSIL(版本控制系统)不添加 .htaccess 文件。为什么?

http - .htaccess 按文件类型限制访问

html - 关于 apache 或 weblogic 面试问题的图像

php - 如何在不对绝对路径进行硬编码的情况下在我的 .htaccess 文件中指定 auto_prepend_file?

php - 需要使用 htaccess 将 php 页面重定向到 html 吗?

ssl - cURL:尝试在 RHEL 7 上安装 kubectl 时出现 SSL 连接错误

Python STDOUT 使用 openssl 子进程归档

apache - 如何使用 Apache mod_proxy 隐藏 url 的端口号?