.htaccess - how to force "www." in a generic way? - .htaccess

This will change domain.example to www.domain.example:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.example$ [NC]
RewriteRule ^(.*)$ `http://www.domain.example/$1` [R=301,L]
How do I replace the "domain" part so that this works on any domain?

I would use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.

This will do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This won't work with subdomains.
domain.example correctly gets redirected to www.domain.example
but
images.domain.example gets redirected to www.images.domain.example
Instead of checking if the subdomain is "not www", check if there are two dots:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)" "http://www.%1$1" [R=301,L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This redirects example.com to www.example.com excluding subdomains.

This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)

this worked like magic for me
RewriteCond %{HTTP_HOST} ^sitename.example [NC]
RewriteRule ^(.*)$ https://www.sitename.example/$1 [L,R=301,NC]

Related

HTTP_HOST RewriteCond doesn't match and doesn't redirect

I have something like this set up in my htaccess, I'm currently trying to redirect an old domain to a new one;
RewriteCond %{HTTP_HOST} ^oldsite\.com [NC]
RewriteRule .* https://www.newsite.com/$1 [L,R=301]
The problem I have, is when I go on "http://www.oldsite.com", I get no redirect.
However, right under this rule is this one, and it does redirect properly to www, what gives?
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can use:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteRule .* https://www.newsite.com/$1 [L,R=301]
Indeed ^ means: in the beginning.
and (www\.)? with www but optionally.

redirect incomplete url to https://

I need to edit my .htaccess so the following happens:
theeatalianjob.com -> https://www.theeatalianjob.com
www.theeatalianjob.com -> https://www.theeatalianjob.com
http://www.theeatalianjob.com -> https://www.theeatalianjob.com
the following:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
only adds the https:// in front of the HTTP_HOST how do I write a if statement that adds www. if missing?
Thank you for your help.
Your answer will do 2 things:
Add www if missing
Redirect http to https
So if URL is http://domain.com then there will be two redirects and that is bad for SEO and for user experience also.
You can use a single rule to do both redirects like this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
I think I have found the solution to my own question but feel free to suggest more elegant ones...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

How to redirect example.com/folder/ to www.example.com/folder/?

I need to know how to redirect example.com/folder/ to www.example.com/folder/ using htaccess. At moment htaccess redirects example.com/folder/ to www.example.com. This is what htaccess says at moment:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder$ /folder/ [L,R]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder/(.*) /$1 [L]
What changes are needed?
Just use this condition:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
First check that the host value is not empty (may be with HTTP/1.0).
The check if we already "use" www and then chrck for SSL (https)
If you want to keep subdomains intact, you should change the second line to this (heads up this is not 100% generic since you have to hardcode in your domain!):
RewriteCond %{HTTP_HOST} ^example.com [NC]

redirect domains and subfolder to new domain htaccess

I have 4 domain which I want 3 of them redirect to last one. I already used some htaccess rules and they are working great,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.com$ [NC]
RewriteRule ^(.*) http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.first.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
the only problem is when I enter "www.first.com/about" it does not shows "www.forth.com/about" just shows the same thing . all the domain are forwarded to forth.com and they do not have hosting so I cannot put htaccess file for those other domains , please guide. would be appreciated.
This is most likely what is happening, at least on my test cases this fix handles it.
Instead of using [L,R=301] reverse the order and do [R=301,L]. The L means Last and it ends cycle on evaluating what to do at that point. I've always used [R=301,L] as are all the examples that I could find anywhere. Potentially your apache may be hitting that L and immediately pass back the new string to the URL parser and isn't sending it as a 301 Redirect as it didn't get that far.
Also on the first.com snippit above you left out the $ on the ^(.*) - only for that line though, not sure if this was a typo or if it's missing in your actual .htaccess file.
Otherwise, you could tidy it up a bit by using:
RewriteCond %{HTTP_HOST} ^((www.)?)first.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)second.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [R=301,L]

.htaccess RewriteRule non-www to www without explicity specifying domain

I'm trying to make a general rewrite rule to redirect all domain.com requests to www.domain.com.
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+])$ [NC]
RewriteRule ^(.*)$ http://www.{HTTP_HOST}/$1 [R=301,L]
The problem is that this rewrite rule doesn't match anything. How can I change it? Thanks
I guess the problem is that there’s an additional ] in your RewriteCond’s pattern and that there is a % missing when referencing HTTP_HOST in RewriteRule’s substitution. So try this:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
And to cover Cags’ concerns a litte bit, you can also try this rule:
RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources