Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess url rewrite http www and non-www to https www + https non-www to https www

I am looking for a set of rewrite rules that will do the following:

  • http www and http non-www to https www
  • https non-www to https www

You could also say i want all url's that aren't equal to https://www.somedomain.com, rewritten to https://www.somedomain.com.

I have seen a lot of answers, but i couldn't find a set of rewrite rules that matches the above and difficulty combining some of them to one solid set of rules that will accomplish the above.

Thanks in advance!

like image 208
Marcellino Bommezijn Avatar asked Jul 23 '26 06:07

Marcellino Bommezijn


1 Answers

You can use this single rule for that in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?somedomain1\.com$
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.somedomain1.com%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} ^(?:www\.)?somedomain2\.com$
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.somedomain2.com%{REQUEST_URI} [NE,R=301,L]
like image 166
anubhava Avatar answered Jul 25 '26 20:07

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!