Results 1 to 9 of 9

Thread: Non-www. to www. 301 redirect

  1. #1

    Non-www. to www. 301 redirect

    Hi guys,

    For some reason, I recently decided to set up 301 redirects on all of my domains so that traffic to the non-www. versions goes to the www. versions.

    I searched around for the best code to use in .htaccess, and the results were all slightly different, but this is what I settled on:

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
    
    ...more rewrite rules...
    It seems to work OK, but are there any potential problems with this setup? I want to be really picky/exact about the use of ! and ^ and so on. I'd like to get it straight before applying it across all of my domains!

    What do you guys do? Thanks for any help.

  2. #2
    Indeed, it seems there are different ways of writing the code. I'm using this one -slightly different from yours- in .htaccess and it works too:

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.domainname\.com$
    RewriteRule (.*) http://www.domainname.com/$1 [R=301,L]
    This was discussed earlier in http://www.netbuilders.org/site-seo/...tter-1437.html

  3. #3
    They both do the same thing, but neither of them are correct. You are redirecting the www domain to the same subdomain and domain which accomplishes nothing. Every page would generate a 301 status code, rather than a proper 200 status code.

    When you entered the sample code shown above, you may entered it incorrectly because that code will not do what you are trying to accomplish.

    This is the correct version

    Code:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]
    This version redirects a non-www domain to a www subdomain and a domain.


    [NC] is supposed to handle mixed case characters (case insensitive) , but it appears to work the same without it. It is possible that it is no longer required with newer versions of Apache. Someone else may know more about the history of this.

    Options +FollowSymLinks is required by some servers. The majority do not need it. I have actually never seen a server configuration that required it.

    RewriteBase / may not normally be required, but use it if the 301 redirect does not work. It states where the relative root directory for the .htaccess file resides. If the .htaccess file and a forum are in a directory called "forum", then the RewriteBase would be /forum/.
    Last edited by TopDogger; 26 July, 2009 at 15:07 PM.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin


  4. #4
    Thanks for the correction TopDogger.

  5. #5
    I'm not quite sure that I understand TopDogger's reply; my code generates a 301 status code when you request any page on the site without putting the www. and correctly generates a 200 status code when you request any page on the site with the www. included. I've checked this using services such as this one.

    Furthermore, have a look at this page on the Apache website (see Canonical Hostnames), and notice that they say to use the fully qualified domain name (i.e., including the www. part) in the RewriteCond (they also use a slightly different first part of the RewriteRule).

    But I'm inexperienced with this; please let me know if I'm confusing something here!

  6. #6
    @Dr. Stavros, are you sure that the code you posted is what you are using? It looks like there is a typo.

    It looks like the RewriteCond is looking for a www domain and rewriting it to a www domain.

    I have never seen it done that way and I've been doing web development for over 10 years.

    The typical standards are:

    to force the use of the www subdomain
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]
    to force the non-www domain
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
    RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
    http://www.seobook.com/archives/001714.shtml

    http://www.tech-evangelist.com/2006/...-301-redirect/

    http://www.seomoz.org/article/guide-...ts-with-apache

    Some of these examples show the RewriteCond statement without escaping the dot with a backslash. The backslash has always been needed when I have set this up.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin


  7. #7
    Hey TopDogger,

    The piece Dr.Stavros has down is perfectly correct. The ! in the front of the expression indicates NOT equal, thus the rewrite works as so - If domain is not www.mydomain.com then redirect to the www.

  8. #8
    I must be blind in one eye and just can't see out the other. I did not see the bang (!) at all.

    Now that I see the bang I see that it should work. That should also redirect all subdomains to the www domain, which could be useful if you are eliminating subdomains.

    Sorry for the confusion, Dr. Stavros.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin


  9. #9
    Hi,

    OK, not seeing the "!" explains it; no worries .

    Indeed, I don't use any subdomains, so yes, if anyone tries coming to my site, but the (fully-qualified) domain name that they request is NOT www.example.com, I forward them to http://www.example.com/whatevertheywanted with a permanent (301) redirect.

    Now I'm a little curious about the slightly different ReWrite rule that Apache recommend...

Similar Threads

  1. [Suggestion] Need a Redirect
    By Sami4u in forum Announcements and Suggestions
    Replies: 8
    Last Post: 11 March, 2011, 17:08 PM
  2. [WTS] WP Redirect Bot
    By iowadawg in forum Scripts
    Replies: 0
    Last Post: 27 December, 2010, 22:26 PM
  3. Non www to www redirect
    By tetrapak in forum Managing
    Replies: 8
    Last Post: 31 May, 2010, 18:19 PM
  4. redirect redirect and redirect again :(
    By gazzele in forum Proxy List Support Forum
    Replies: 2
    Last Post: 5 January, 2010, 22:47 PM
  5. 301 redirect all to all
    By Hellas in forum Managing
    Replies: 0
    Last Post: 27 August, 2009, 12:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •