.htaccess:如果还更改根路径,则将域重定向到https不起作用

我有以下域名:</ p>

  example1.com \  nexample2.com 
example3.com
</ code> </ pre>

域名指向 / public_html / </ code>。 我想在 /public_html/.htaccess </ code>中做三件事:</ p>


  1. 重定向(包含所有参数和路径) 域 example2.com </ code>和 example3.com </ code>到域 example1.com </ code>。</ p> </ li>

  2. < 总是使用 https </ code>和 www </ code>来显示 example1.com </ code>本身(如果没有,然后重定向),意思是: https: //www.example1.com

  3. 域名的自定义根路径为 / public_html / </ code>。 我想将其更改为 / public_html / example_path / </ code>。</ p> </ li>
    </ ol>

    我在 / public_html中有以下内容 /.htaccess:

    RewriteCond%{HTTP_HOST} ^ example2.com [NC,OR] 
    RewriteCond%{HTTP_HOST} ^ www.example2.com [NC ]
    RewriteRule ^(。*)$ https://www.example1.com/$1 [L,R = 301,NC]

    RewriteCond%{HTTP_HOST} ^ example3.com [NC,OR]
    RewriteCond%{ HTTP_HOST} ^ www.example3.com [NC]
    RewriteRule ^(。*)$ https://www.example1.com/$1 [L,R = 301,NC]

    RewriteCond%{HTTP_HOST} ^ example1。 com [NC]
    RewriteRule ^(。*)$ https://www.example1.com/$1 [L,R = 301,NC]

    RewriteCond%{HTTP_HOST} ^ example1.com $ [NC,OR]

    RewriteCond%{HTTP_HOST} ^ www.example1.com $
    RewriteCond%{REQUEST_URI}!example_path /
    RewriteRule(。*)/ example_path / $ 1 [L]
    </ code> </ pre>

    几乎按预期工作。 但是当打开 http://www.example1.com </ code>时,没有重定向到 https://www.example1.com </ code>。 这仅在删除最后四个代码行时起作用,这应该改变根路径。</ p>

    对于所有其他域,它正在工作。</ p>

    为什么不是 这是为了 http://www.example1.com </ code>吗? 为什么它只在不改变根路径时才起作用?</ p>
    </ div>

展开原文

原文

I have the following domains:

example1.com
example2.com
example3.com

The domains point to /public_html/. There are three things I want to do in /public_html/.htaccess:

  1. Redirect (with all parameters and paths) the domains example2.com and example3.com to the domain example1.com.

  2. example1.com itself should get shown (if not, then redirect) always with https and www, means: https://www.example1.com

  3. The custom root path for the domains is /public_html/. I want to change this to /public_html/example_path/.

I have the following in /public_html/.htaccess:

RewriteCond %{HTTP_HOST} ^example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example2.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^example3.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example3.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]

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

RewriteCond %{HTTP_HOST} ^example1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]

This is nearly working as expected. But when opening http://www.example1.com there is no redirection to https://www.example1.com. This is only working when removing the last four code lines, that should change the root path.

For all other domains it's working.

Why isn't it woking for http://www.example1.com? And why is it only working when not changing the root path?

douaipi3965
douaipi3965 我的意思是JarekBaran在这里写下的内容。您只需要检查它是否与您想要的结果不同,而不是逐个检查输入的域。前三个规则合并为一个。
大约一年之前 回复
duanchi0897
duanchi0897 你的意思是,我只需要一行RewriteRule^(。*)$https://www.example1.com/1[L,R=301,NC]或者,你的意思是什么?
大约一年之前 回复
dth2331
dth2331 为什么不将条件与相同的规则结果相结合?前三条规则可以统一。
大约一年之前 回复
donglaohua1671
donglaohua1671 对不起,这是一个错误。我改变了我的意思是example1.com。
大约一年之前 回复

1个回答

Add this rule to yours at the top of the .htaccess file

# http <> https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]

Or check these rewrites instead of your rules:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://www.example1.com/$1 [R=301,L]

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

    RewriteCond %{REQUEST_URI} !example_path/
    RewriteRule (.*) /example_path/$1 [L]

</IfModule>

For specific domains (www/non-www example-other.co, www/non-www example3.com, www/non-www example2.com, example1.com)

<IfModule mod_rewrite.c>
    RewriteEngine On

    # http www/non-www example-other.com/any to https://www.example-other.com/any
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?example-other\.com [NC]
    RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]

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

    # http www/non-www example1.com/any to https://www.example1.com/any
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com [NC]
    RewriteRule (.*) https://www.example1.com/$1 [R=301,L]

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

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

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

    # Rewrite /any to /example_path/any 
    RewriteCond %{REQUEST_URI} !example_path/
    RewriteRule (.*) /example_path/$1 [L]

</IfModule>
douzhang8033
douzhang8033 我说的是,例如 example-other.com。 这也应该重定向到https:// www但不能重定向到域example1.com。 我怎样才能做到这一点?
大约一年之前 回复
dongsisui7562
dongsisui7562 我为各个域添加了一个示例。
大约一年之前 回复
duanmen1887
duanmen1887 还有一些我不想重定向到http://www.example1.com域名。 我该如何排除它们?
大约一年之前 回复
duanheye7423
duanheye7423 它将所有http://请求重定向到https:// ex。 http://www.example1.comhttps://www.example1.com/
大约一年之前 回复
doudou6719
doudou6719 第一个是工作,谢谢。 但为什么? 它在做什么?
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问