Mobile app version of vmapp.org
Login or Join
Hamaas447

: Redirect non-www to www while preserving protocol I am aware of the fact that there are tons of questions in this section and in server fault dealing with re-directions from non-www to www

@Hamaas447

Posted in: #Https #ModRewrite #NoWww

I am aware of the fact that there are tons of questions in this section and in server fault dealing with re-directions from non-www to www URLs. But I couldn't find one dealing with this issue while preserving protocol.

I am no mod-rewrite expert, and my code is just copy/pasted... Here's what I have:

RewriteCond %{HTTP_HOST} ^domain.tld$ [NC]
RewriteRule ^(.*)$ www.domain.tld [R=301,L]


So now domain.tld and domain.tld are forwarded to www.domain.tld. How do I make it so that HTTPS stays on HTTPS while HTTP stays on HTTP?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Hamaas447

2 Comments

Sorted by latest first Latest Oldest Best

 

@Debbie626

You can use the code you have, just use it again with HTTPS_HOST too (more chars escaped with even though it may not be needed in most servers). As w3d noted in a comment above, this is designed to work in a htaccess file because of the / before the :

RewriteCond %{HTTP_HOST} ^domain.tld$
RewriteRule ^(.*)$ "http://www.domain.tld/" [R=301,L]

RewriteCond %{HTTPS_HOST} ^domain.tld$
RewriteRule ^(.*)$ "https://www.domain.tld/" [R=301,L]


Here is the one we use sometimes to cover addon domains situations. Consider most addon domains use a utility subdomain on a host domain, which is often accessible. This would also forward the sub and www-sub:

RewriteCond %{HTTP_HOST} ^domain.hostdomain.tld$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.hostdomain.tld$ [OR]
RewriteCond %{HTTP_HOST} ^domain.tld$
RewriteRule ^(.*)$ "http://www.domain.tld/" [R=301,L]

RewriteCond %{HTTPS_HOST} ^domain.hostdomain.tld$ [OR]
RewriteCond %{HTTPS_HOST} ^www.domain.hostdomain.tld$ [OR]
RewriteCond %{HTTPS_HOST} ^domain.tld$
RewriteRule ^(.*)$ "https://www.domain.tld/" [R=301,L]


There are methods to make these combined as 1 rule-set, but it's nicer IMO to keep them as separated HTTP and HTTPS "routers". You can play with the targets in each if you wanna enforce either-or SSL mode too.

10% popularity Vote Up Vote Down


 

@Bryan171

Just a little information. Best practices indicate that trafic from HTTP has to be redirected to HTTPS. If your site is reachable on HTTPS, no more need to HTTP.

I think you can associate no-www redirection to www with HTTP redirection to HTTPS.

Moreover, be careful of duplicate content if your site is reachable with HTTP and HTTPS.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme