Redirecting from non-www to www domains in Apache

AID: 195
  • Status: Published

5570 points

  • ByShaneJones
  • TypeGeneral
  • Posted on2008-11-20 at 17:15:21
In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions.

For instance redirecting
http://domain.com
to
http://www.domain.com

From a Search Engine Optimisation perspective I recommend this as Search Engines may see this as 2 separate URLs serving the same content. In this case we would need to perform a server side 301 redirect. We use a 301 redirect for this because if any links that are passing PR have been pointed to the non-www version it will be transferred over time to the www version of the URL. I you were to use a 302 redirect you would not pass this important information over to the www version.

To create this redirect is simple. on your server you need to locate your .htaccess file and open it in your choice of text editor.

For going from non-www to www you will need to use the below lines and change domain.com for your domain name and TLD.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
                                    
1:
2:
3:

Select allOpen in new window



This will then work on all pages within your site for instance
http://domain.com/somefolder/anotherone/
would redirect to
http://www.domain.com/somefolder/anotherone/

If however you wanted to remove the www's from all of your URL's you would use the below lines  and also change the domain and TLD.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
                                    
1:
2:
3:

Select allOpen in new window



Shane Jones
Asked On
2008-11-20 at 17:15:21ID195
Tags

Apache

,

non-www

,

www

,

non-www to www

Topic

Apache Web Server

Views
4890

Comments

Expert Comment

by: shrishti132 on 2009-07-20 at 03:12:20ID: 2167

Hi,

I was searching for some PAQs regarding non-www to www redirects for Apache. All talk about .htaccess redirect. But, my host does not allo mod-rewrite. Do you have any suggestions for this? If I upload .htaccess file, my website gives "internal server error".

Thanks.

Expert Comment

by: cduke250 on 2010-09-08 at 14:11:11ID: 19199

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

Select allOpen in new window



It's a bit safer to use a negation instead, otherwise you won't catch ww.domain.com or w.domain.com.

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Apache Web Server Experts

  1. ahoffmann

    74,014

    Master

    0 points yesterday

    Profile
    Rank: Genius
  2. testez

    66,765

    Master

    0 points yesterday

    Profile
    Rank: Guru
  3. arnold

    58,411

    Master

    0 points yesterday

    Profile
    Rank: Genius
  4. Ray_Paseur

    48,568

    0 points yesterday

    Profile
    Rank: Savant
  5. DaveBaldwin

    48,088

    0 points yesterday

    Profile
    Rank: Genius
  6. xterm

    37,235

    1,000 points yesterday

    Profile
    Rank: Sage
  7. routinet

    36,750

    0 points yesterday

    Profile
    Rank: Genius
  8. jason1178

    22,265

    0 points yesterday

    Profile
    Rank: Genius
  9. arober11

    21,914

    20 points yesterday

    Profile
    Rank: Sage
  10. COBOLdinosaur

    19,164

    0 points yesterday

    Profile
    Rank: Genius
  11. ravenpl

    17,268

    0 points yesterday

    Profile
    Rank: Genius
  12. larsrohr

    15,900

    0 points yesterday

    Profile
    Rank: Guru
  13. maeltar

    15,300

    0 points yesterday

    Profile
    Rank: Guru
  14. for_yan

    15,050

    0 points yesterday

    Profile
    Rank: Genius
  15. KeremE

    14,601

    1,000 points yesterday

    Profile
    Rank: Genius
  16. giltjr

    14,360

    0 points yesterday

    Profile
    Rank: Genius
  17. maestropsm

    13,955

    0 points yesterday

    Profile
    Rank: Guru
  18. Tiggerito

    12,064

    0 points yesterday

    Profile
    Rank: Sage
  19. legolasthehansy

    11,820

    0 points yesterday

    Profile
    Rank: Guru
  20. smadeira

    11,652

    0 points yesterday

    Profile
    Rank: Wizard
  21. hernst42

    11,400

    0 points yesterday

    Profile
    Rank: Genius
  22. Roads_Roads

    10,630

    0 points yesterday

    Profile
    Rank: Genius
  23. gr8gonzo

    10,600

    0 points yesterday

    Profile
    Rank: Sage
  24. maxmk

    10,168

    0 points yesterday

    Profile
  25. Anuroopsundd

    9,936

    0 points yesterday

    Profile
    Rank: Sage

Hall Of Fame