问题描述
我想重定向以下条件:
http://mydomain.com
http://www.mydomain.com
到
https://mydomain.com
但我不希望它搞糟与任何子域,这样的人谁类型:
But I don't want it to mess up anything with subdomains, so that someone who types:
将不会被重定向到https。我的present htaccess文件看起来是这样的:
would not be redirected to https. My present htaccess file looks like this:
# 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
我试着用一些我在这里发现堆栈溢出的例子,像这样的:
I've tried using some of the examples I've found here on stack overflow, such as this one:
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
不过,尽管它似乎已经工作了的人,它导致了重定向循环对我来说。顺便说一句,当我得到这个工作,我需要添加一些重定向来处理子域,以无形(不更改URL)重定向 HTTP ://m.mydomain.com 以 http://mydomain.com/m 为例。因此,无论是在这里完成的不应该发生的prevent了。
But although it appears to have worked for that person, it resulted in a "redirect loop" for me. As an aside, after I get this working I need to add some redirects to handle subdomains, to invisibly (without changing the URL) redirect http://m.mydomain.com to http://mydomain.com/m for example. So whatever is done here shouldn't prevent that from happening.
如果有帮助,该网站托管在Rackspace的云网站
If it helps, the site is hosted on Rackspace Cloud Sites
任何人都知道如何做到这一点?
Anybody know how to do this?
谢谢!
修改
我尝试这样做:
#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]
这似乎在大多数情况下,当我输入一个子文件夹似乎不工作的工作,不同的是。例如,如果我输入这个到地址栏:
It seems to work for the most part, except that when I type in a subfolder it doesn't seem to work. For example, if I type this into the address bar:
mydomain.com/temp
它解析为:
http://mydomain.com/temp/
编辑2
嗯,我已经取得了一些进展。到目前为止,我有WWW重定向到非www,和子工作(尽管地址栏没有改变 - 我想这是可以接受的)。如果我把任何形式的强制HTTPS的有什么似乎搞砸了的。这似乎与WWW到非www块冲突。我可以有一个或另一个。有没有一种方法,使这两个部分协同工作?
Well I've made some progress. So far I have www redirecting to non-www, and subdomains working (though the address bar does change - I guess that's acceptable). What seems to mess it up is if I put any sort of forced-https in there. It seems to conflict with the www to non-www block. I can have one or the other. Is there a way to make those two parts work together?
此外,道preSS的网站地址和字preSS地址都设置为 https://mydomain.com
Also, the WordPress site Address and WordPress Address are both set to 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公司,我相信。他们有,我发现,通过自己的知识基础寻找自己的preferred方法。下面是一个伟大工程的最终htaccess文件。所有的WWW网址被发送到非www的网址,HTTP被发送到https,与子域名重定向到合适的子目录中,没有搞乱字preSS:
For most web hosts, anubhava's solution would work just fine. However, the key here turned out to be Rackspace, I believe. They have their own preferred methods that I found by searching through their knowledge base. Here's the final htaccess file that works great. All www urls get sent to non-www urls, http gets sent to https, and subdomains redirect to their proper subdirectories, without messing up 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
非常感谢这一切你的帮助。
Thanks so much for all your assistance with this.
推荐答案
您可以有你的.htaccess这样的:
You can have your .htaccess like this:
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云环境实施看看用于HTTPS的Rackspace的云计算正确的变量是 ENV:HTTPS
。
PS: Note that the HTTPS server variable is not correctly implemented in a Rackspac Cloud Environment. The correct variable to look at for HTTPS in Rackspace Cloud is ENV:HTTPS
.
这篇关于htaccess的重定向HTTP://和http:// WWW到https://播放漂亮与子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!