简体   繁体   中英

mod_rewrite to ssl not working for example.com but does for example.com/index.html

NOTE: config and error updated to be clearer.

I have the following .htaccess for my site running apach 2.4 on Ubuntu:

<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^(www\.)example1.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)example2.com
RewriteRule ^(.*)$ https://www.example2.com/$1 [L,R=301]
</IfModule>

Is there something I am missing so that it will redirect with just the base / url?

The problem is these two rules:

RewriteCond %{HTTP_HOST} ^(www\.)example1.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)example2.com

You need to add ? to make the www. optional:

RewriteCond %{HTTP_HOST} ^(www\.)?example1.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example2.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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