繁体   English   中英

.htaccess:如果还更改了根路径,则无法将域重定向到https,

[英].htaccess: Redirect domain to https not working if also changing the root path

我有以下域:

example1.com
example2.com
example3.com

域指向/public_html/ 我想在/public_html/.htaccess做三件事:

  1. 将域example2.comexample3.com (以及所有参数和路径)重定向到域example1.com

  2. example1.com本身应始终通过httpswww显示(如果没有,则重定向),这意味着: https://www.example1.com : https://www.example1.com

  3. 域的自定义根路径是/public_html/ 我想将其更改为/public_html/example_path/

我在/public_html/.htaccess有以下/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]

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

对于所有其他域,它都有效。

为什么http://www.example1.com无法正常显示? 为什么只在不更改根路径的情况下才起作用?

将此规则添加到.htaccess文件顶部的规则中

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

或者检查这些改写而不是您的规则:

<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>

对于特定域(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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM