Using .htaccess to forward domain.com to www.domain.com

Archive

Mo Tu We Th Fr Sa Su
1234
567891011
12131415161718
19202122232425
262728293031


Poll: Indonesia

Masihkah Indonesia lebih baik ?

  • email Email to a friend
  • print Print version
  • Add to your del.icio.us del.icio.us
  • Digg this story Digg this

Did you enjoy this article?

(total 0 votes)
Adjust font size: Decrease font Enlarge font
image Perhaps you have bought a new domain name or need to change a filename without losing links which are pointing to it or maybe you have just changed the structure of your website. Regardless, your search engine rankings could take a real hit unless you make sure that people can still find your pages.

There are a couple of ways to achieve a redirect. Some are good and some are not so good, here we try to make some sense of it for you.

   1. meta http-equiv=”refresh” - Please, please don’t do this. It looks really bad to both users and search engines. You should only do this if your host supports nothing but HTML and you have no access to the .htaccess file.
   2. 301 redirect - Using a .htaccess file you can announce to the search engines that your page has moved and your users will know no different.

How do we create 301 redirects then?

Create a file (notepad?) called .htaccess - that is {DOT}htacces. From here there are a few ways forward so I will list them seperately

    * Redirect 301 /folder http://domain.com/folder = This will direct a user from http://server.com/folder/file.html to http://domain.com/folder/file.html (If you wish you can perform the same action in other ways: RedirectPermanent /folder http://domain.com/folder)
    * RedirectMatch (.*)\.html$ http://server.com$1.htm = This example matches any character ‘.’ any amount of times ‘*’. That is to say that it will match anything. So in this example the user requests file.html and the directive in the .htaccess file matches it and redirects the client to http://server.com/file.htm
    * RewriteEngine ON
      RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L] = This example re-maps any previous domain to your new domain. So any user trying to view http://www.olddomain.com/index.html will be forwarded to http://www.newdomain.com
    * RewriteRule /.* http://www.newdomain.com/ [R=301,L] = This example meas that no matter what page the user attempts to access, they will be redirected to the http://www.newdomain.com address.

To provide an example, here is an .htaccess file of ours which forwards some of our domains from http://domain.com to http://www.domain.com (ie it adds the www.) you can test this here: http://gembels.com/ or http://www.gembels.com/

Example .htacess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^gembels\.com
RewriteRule ^(.*)$ http://portal.gembels.com/$1 [R=permanent,L]

You should then place your .htaccess file into the root of your web folder (for example, cpanel users this will be : /home/username/public_html/) You can then test your changes.
  • email Email to a friend
  • print Print version
  • Add to your del.icio.us del.icio.us
  • Digg this story Digg this

Post your comment comment Comments (0 posted)

© 2000-2008 Gembels.com by dono a.k.a Widhe