htaccess:重定向所有子域到主https网域
我卡住使用htaccess重定向,混合一些答案[1][2]关于htaccess重定向。htaccess:重定向所有子域到主https网域
我的目标是将每个子域(不是www)重定向到我的主域(从https://www开始),保持Request_URI(如指定的文件夹)原样。
这里是我做了什么:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.fr$ [NC]
# OR : RewriteCond .* ^(.+)\.%{HTTP_HOST}%{REQUEST_URI}$ [NC]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.fr$ [NC]
RewriteRule^https://www.mydomain.fr%{REQUEST_URI} [L,R=301,QSA]
这个配置不重定向(强文是坏的重定向):
- test.mydomain.fr =>https://www.mydomain.fr
- 测试。 mydomain.fr/foo =>https://www.mydomain.fr/foo
- mydomain.fr =>https://www.mydomain.fr
- https://foo.mydomain.fr =>https://foo.mydomain.fr
- www.mydomain.fr =>http://www.mydomain.fr
交换 RewriteCond %{HTTP_HOST} !^www\.mydomain\.fr$ [NC]
到
RewriteCond %{HTTP_HOST} !^https://www\.mydomain\.fr [NC]
给我坏重定向的错误。 我的想法是要检查HTTP_HOST是格式https://www.mydomain,如果不是重定向到与请求信息的良好URL RewriteRule^https://www.mydomain.fr/%{REQUEST_URI}
为什么会有不好的重定向? 我是否必须编写2组条件和规则来首先检查子域,然后HTTPS? (目前,我试图让使用一个规则和几个条件重定向,这可能不是最好的做法)
编辑:我的目标是让这些重定向:
- test.mydomain.fr =>https://www.mydomain.fr
- test.mydomain.fr/foo =>https://www.mydomain.fr/foo
- mydomain.fr =>https://www.mydomain.fr
- https://foo.mydomain.fr =>https://www.mydomain.fr
- www.mydomain.fr =>https://www.mydomain.fr
在此先感谢您的任何答案/想法。
编辑2:
我试过在THIS问题Larzan答案,这似乎做我想做的只是一件事是什么:如果我在我的浏览器的网址是一样的东西https://foobar.mydomain.fr
,浏览器显示了我关于安全的大红色警告,我必须接受风险,那么重定向到https://www.mydomain.fr
。我如何删除此警告?
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.mydomain
RewriteRule ^(.*)$ https://www.mydomain.fr%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
对单个规则替换您的两个规则:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.fr$ [NC]
RewriteRule^https://www.mydomain.fr%{REQUEST_URI} [L,R=301,NE]
测试后彻底清除浏览器缓存中的变化。
嗨,这个解决方案修复了重定向'www.mydomain.fr' =>'https:// www.mydomain.fr',但是如果我在我的资源管理器中输入:'https:// foobar.domain.fr',URL仍然是一样的。输入的URL不以www开头,所以应该接受重定向。但似乎没有。 “www”!=“foobar”为什么Apache看不到?谢谢 – Marcassin
此规则也适用于子域,但子域应该指向与主域的DocumentRoot相同的目录。 – anubhava
有没有办法重定向每个子域(通配符)?所以没有目录声明为子域,只为主域。 (参看编辑2)。谢谢 – Marcassin
“https://foo.mydomain.fr => https:// foo.mydomain.fr”是什么意思?两个URL都是一样的,什么都没有发生? –
我编辑了我的问题来添加我想要的重定向。 @DusanBajic:我想压制子域foo。 – Marcassin
@anubhava:我想去尝试httpS而不是http – Marcassin