htaccess redirect no-www to www (multidomains) - .htaccess

I need to redirect non www URL to WWW URL for multidomains
here is what I need exactly
http://example.com should goto www.example.com
http://example.de should goto www.example.de
All the above geographilay domains should do to their corresponding domains with www (.com .de and .uk are in same server)

Try this :
RewriteEngine On
RewriteCond %{HTTP_POST} !^www\. [NC]
RewriteCond %{HTTP_POST} ^(([^\.]+)\.([a-z\.]+))$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

All credit go to MrWhite his solution here:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

Force WWW to use HTTPS

How do I force www to use https and not just http?
For example, when visiting http://www.example.com it redirects to https://www.example.com but if you don't include the www, you don't get redirected to the SSL version.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.(.*)$
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/crossdomain.xml
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Below condition should work for both www and without it.
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]

Redirect NON-www to www for https

I've the following redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How can I redirect non WWW https to WWW?
The htaccess is located in the public_html folder
You can use the following :
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will redirect
http://example.com
https://example.com
to
https://www.example.com
I'm using this on a Joomla site and it redirects everything to https://www
I can't see this solution anywhere else so thought I'd share it
RewriteCond %{HTTP_HOST} ^domainname\.co.uk [NC]
RewriteRule ^(.*)$ https://www.domainname.co.uk/$1 [R=301,L]

how to redirect www and non www to https for specific domain using htaccess

I need to redirect www and non www to https. I have looked everywhere on stackoverflow but can't find quite what I'm looking for.
The rules are:
example.com and www.example.com and https://example.com
must redirect to https://www.example.com
It must match the domain and extension example.com. It cannot be a wild card which also
matches abc.com or 123.com or example.net or anything else
It must match for www subdomains only. It cannot be a wild card which
also matches sub.example.com or thisisademo.example.com
I currently have:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
However if someone enters www.example.com it still goes to the http version instead of https.
What I think I actually need here is RewriteCond regex to match exactly and only "example.com" and "www.example.com"
Thanks
You can use the following rule to redierct non-www or www to https://www in just one redirection
#redirect http non-www to https://www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
#redirect https non-www to www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Clear your browser's cache before testing this rule.
This is the only way it works for me - with !on instead of off and with %{ENV:HTTPS} instead of %{HTTPS}:
#non-www. http to www. https
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#non-www. https to www. https
RewriteCond %{ENV:HTTPS} on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#First rewrite any request to the wrong domain to use the correct one (i.e. www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Configuring .htaccess for multiple domains and SSL

I have a primary domain "example.co.uk" and 6 other domains + 1 IP address pointed to the same folder root.
I would like to use HTTPS for the primary domain i.e. https://www.example.co.uk and redirect all the other domains to the homepage of this domain (avoiding SEO issues and content duplication with the example.com domain and IP address)
How do I achieve this using my current .htaccess file?
RewriteEngine on
#Redirect IP address to example.co.uk.
RewriteCond %{HTTP_HOST} ^12\.34\.567\.890
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]
# Redirect example.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example2.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example3.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example4.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example4\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example5.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example5\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example6.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example6\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
You can reduce the number of rules by negating the condition check against the HTTP HOST. Then, you'll just need the rule to check HTTPS:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
# redirect all the other domains to the homepage
RewriteCond %{HTTP_HOST} !^www\.example.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/ [L,R=301]
In your question, you said you wanted to redirect all the other domains to the homepage of this domain, but your rules aren't doing that at all. Your rules are redirecting the request to the same request but just this domain. If you want to keep that logic, then you need to add a $1 to the end of the https://www.example.co.uk/.

redirecting www and non-www to www2 subdomain

This is the code I currently have:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www2.domain.com/$1 [R=301,L]
and this redirects www.domain.com to www2.domain.com properly. I want the non-www to redirect to like ^www.(.*)$ [NC] does since it keeps urls intact and just let them sit on www2.domain.com/whatever instead.
I think you need this
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*) http://www2.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www2.domain.com/$1 [R=301,L]
This works well, what if we only wanted to redirect the domain name to www2 for example we just want to redirect www.domain.com but not www.domain.com/folder?

Resources