简体   繁体   中英

Force SSL + www + Subdirectory with .htaccess

I have the following domains:

http://example.net    -> https://www.example.com/
http://example.org    -> https://www.example.com/
http://example.com    -> https://www.example.com/
http://theexample.com -> https://www.example.com/

I'm lost looking for a way to be able to force redirect all these domains to a single domain that uses www and https including all subdirectories. The main domain is example.com and I need to redirect it including subdirectories like:

http://example.net/blog     -> https://www.example.com/blog
http://www.example.org/blog -> https://www.example.com/blog
https://example.com/blog    -> https://www.example.com/blog
http://theexample.com/blog  -> https://www.example.com/blog

https://www and the domain example.com is mandatory all subdirectories.

Thank you in advance for your help

Rather than checking for the hostnames to redirect, you could instead just check that the scheme + hostname is not the canonical and then redirect to it. So, if the request is not for https://www.example.com/... then redirect (copying the URL-path).

For example:

RewriteEngine On

RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule (.*) https://www.example.com/$1 [R=302,L]

The above states... for all requests where HTTPS is either off or not www.example.com then redirect to https://www.example.com/<URL-path> . $1 is a backreference to the capturing subgroup in the RewriteRule pattern (ie. the URL-path). Any query string is also copied through by default.

The = prefix on the CondPattern makes this an exact string match, not a regex. And the ! prefix negates the regex (ie. does not match).

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