220

I have this in my .htaccess file:

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]

but whenever I access a file on my root like http://example.com/robots.txt it will redirect to http://www.example.comrobots.txt/.

How can I correct this so that it will redirect correctly to http://www.example.com/robots.txt?

13 Answers 13

452

Change your configuration to this (add a slash):

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 

Or the solution outlined below (proposed by @absiddiqueLive) will work for any domain:

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

If you need to support http and https and preserve the protocol choice try the following:

RewriteRule ^login\$ https://www.%{HTTP_HOST}/login [R=301,L]

Where you replace login with checkout.php or whatever URL you need to support HTTPS on.

I'd argue this is a bad idea though. For the reasoning please read this answer.

9
  • This solution doesn't work when some pages (like login or sign up) use https protocol. Nov 27, 2014 at 7:47
  • I've updated the post slightly to include some info. It's hard to both generically and correctly choose the protocol to use. Nov 28, 2014 at 8:12
  • 4
    The first solution doesn't redirect enough, the 2nd seems to redirect too much. For example blog.example.com becomes www.blog.example.com
    – gman
    Jun 5, 2015 at 10:02
  • Could you please also add how to avoid the redirect while developing on localhost?
    – Bugs Bunny
    Oct 25, 2015 at 18:42
  • 1
    ive been following everything on here but nothing is working. Im using a laravel setup. Does anyone have any idea why this isnt working for me? www.domain.com and domain.com both load properly but domain.com doesnt redirect to www.domain.com. Could this be a problem with my DNS? Feb 4, 2016 at 3:40
113

Here's the correct solution which supports https and http:

# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

UPD.: for domains like .co.uk, replace

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$

with

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.[^.]+$
10
  • 7
    This is perfect, it also doesn't redirect subdomains to www like the other generic solutions do. Apr 7, 2015 at 9:10
  • 4
    This is a best solution, works much better than the accepted answer, and takes care of HTTPS properly Jun 9, 2015 at 8:51
  • 3
    Works perfectly! And in such few lines!
    – zeckdude
    Nov 29, 2015 at 3:19
  • 10
    If I am not mistaken, this wouldn't work for more than one dot. Examples: yoursite.co.uk Jan 12, 2016 at 14:02
  • 1
    Doesn't work for me. When I type domain.de it takes me to https://domain.de. But I want https://www.domain.de
    – molerat
    Sep 8, 2016 at 18:33
88
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.

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

For Https

RewriteCond %{HTTPS}s ^on(s)|

RewriteRule ^(.*)$ http%1://www.%{HTTP_HOST}/$1 [R=301,L]
6
  • 8
    This solution is preferred to @ranman's as it will work for all domains thus minimizing human error either when you're implementing code for a new project or when you implement extra-domains to an existing project.
    – Frankie
    Aug 20, 2014 at 15:16
  • I prefer this solution, but why ^(.*)$ instead of (.*)?
    – Popnoodles
    Sep 8, 2014 at 17:03
  • this solution is better but i have edited mine to contain yours as well. Nov 4, 2014 at 0:59
  • 1
    This is working for all domain its simple and effective! Thank you Mar 25, 2015 at 6:41
  • you may follow RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}/$1 [R=301,L]
    – abSiddique
    Sep 20, 2015 at 5:59
27

The following example works on both ssl and non-ssl and is much faster as you use just one rule to manage http and https

RewriteEngine on


RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

[Tested]

This will redirect

http

to

https

to

4
  • 1
    Very close to what I came up with. Supports more than one dot in the HTTP host too, for example "mysite.co.uk".
    – Adambean
    Jul 7, 2016 at 11:58
  • 2
    Just out of curiosity, why aren't you using R=301 here? It will use 302 (moved temp.) by default if no code specified.
    – Marc
    Dec 4, 2016 at 20:44
  • 1
    Works perfectly. This should be the accepted answer. Sep 29, 2018 at 22:20
  • it will also redirect blog.domain.com to www.blog.domain.com, which is not always desired Feb 4, 2021 at 16:14
9

Try this, I used it in many websites, it works perfectly

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^bewebdeveloper.com$
RewriteRule ^(.*) http://www.bewebdeveloper.com/$1  [QSA,L,R=301]
7

I have tested all the above solutions but not working for me, i have tried to remove the http:// and won't redirect also removed the www it redirect well, so i get confused, specially i am running all my sites under https://

So i have combined some codes together and came up with perfect solution for both http:// and https:// and www and non-www.

# HTTPS forced
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

Hope this can help someone :)

3

Add the following code in .htaccess file.

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

URLs redirect tutorial can be found from here - Redirect non-www to www & HTTP to HTTPS using .htaccess file

3

This configuration worked for me in bitnami wordpress with SSL configured :

Added the below under "RewriteEngine On" in file /opt/bitnami/apps/wordpress/conf/httpd-app.conf

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
2
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

This will redirect your domain which is not started with WWW It is not redirect your all sub domain.

It is useful.

2

I believe the top answer successfully redirects non-www to www (ex: mysite.com -> www.mysite.com), but doesn't take into account wildcard subdomains, which results in:

random.mysite.com -> www.random.mysite.com

Here's a solution with/without HTTPS

HTTP

RewriteEngine On

RewriteCond %{HTTP_HOST} !www.mysite.com$ [NC]
RewriteRule ^(.*)$ http%{ENV:protossl}://www.mysite.com/$1 [L,R=301] 

HTTP/HTTPS

RewriteEngine On

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=protocol:https]

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=protocol:http]

RewriteCond %{HTTP_HOST} !www.mysite.com$ [NC]
RewriteRule ^(.*)$ %{ENV:protocol}://www.mysite.com/$1 [L,R=301]

*note: I haven't tested https because I don't currently have a cert to test, but if someone can verify or optimize what I have that would be awesome.

1
  • What does the first $ do here: '^(.*)$' Jun 29, 2018 at 6:37
1

If possible, add this to the main Apache configuration file. It is a lighter-weight solution, less processing required.

<VirtualHost 64.65.66.67>
        ServerName example.com
        Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost 64.65.66.67>
   ServerAdmin [email protected]
   ServerName www.example.com
   DocumentRoot /var/www/example
   .
   .
   . etc

So, the separate VirtualHost for "example.com" captures those requests and then permanently redirects them to your main VirtualHost. So there's no REGEX parsing with every request, and your client browsers will cache the redirect so they'll never (or rarely) request the "wrong" url again, saving you on server load.

Note, the trailing slash in Redirect permanent / http://www.example.com/. Without it, a redirect from example.com/asdf would redirect to http://www.example.comasdf instead of http://www.example.com/asdf.

1

Two warnings

Avoid 301 and prefer modern 303 or 307 response status codes.

Avoid 301

Think carefully if you really need the permanent redirect indicated as [R=301] because if you decide to change it later, then the previous visitors of the page will continue to see the page of the original redirection.

The permanent redirection information is frequently stored in the browser's cache and, in general, it is difficult to eliminate (reload the page do not solve the problem). Your website visitors will be stuck in the previous redirect "forever".

Avoid 302 too

The new version of the HTTP protocol (v1.1) added two new response status codes that can be used instead of 302.

  • 303 URL redirection but demanding to change the type of request to GET.
  • 307 URL Redirection but demanding to keep the type of request as initially sent.

You can still use the code 302 (non-permanent redirection) although it is considered ambiguous. In any case, most browsers implement 302 in the same way the new 303 code instructs.

1
  • regarding your warning about 301, this will apply even if we are talking about the same domain name? not so sure how this will apply in this case ... redirecting all URL's from http://example.com/testing to http://www.example.com/testing. Could be a bad approach in long term? Thanks,
    – typo_
    Jul 23, 2018 at 16:41
-2

Write in .htaccess :)

## Redirect from non-www to www (remove the two lines below to enable)
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
1
  • 2
    Same as other answers
    – Ankit
    Jun 2, 2017 at 9:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.