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

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?

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>