Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have search through Google and found 2 methods to add www before domain name. First is by adding a CNAME and second is using rewrite mod in .htaccess file.

I just want to know which method is better to use or there is other method because I'm currently using standalone WordPress as my back end.

share|improve this question

migrated from stackoverflow.com Nov 20 '11 at 0:51

This question came from our site for professional and enthusiast programmers.

Adding a CNAME will not create a redirection from domain.com to www.domain.com. Anyway, the .htaccess method is usually fine – Pekka 웃 Oct 31 '11 at 14:42
up vote 5 down vote accepted

You need both - the CNAME is a DNS record that points www.example.com to example.com's server so that if a browser tries to open www.example.com then it can find what server (IP address) it is stored on. The second is a rule on the server that says "if someone loads example.com, tell them that they should have loaded www.example.com"

share|improve this answer

in case you don't use CNAME records, htaccess can redirect too. In your FTP root (maybe in public_html) there should be a file called .htaccess. Open it and add this code into it (probably after the first line, before other rules start).

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

If there is no such file, then create a simple new text file with Notepad and upload to the website root directory, and rename it to .htaccess.

This is a 301 redirect (which is permanent redirect.) After a few weeks the site will be visible with www in Google Search anymore.

share|improve this answer
The question is possibly a bit ambiguous, as it doesn't specifically mention redirects, but anyway... as mentioned in the other answer, you still need a CNAME record as otherwise the www subdomain won't be available. Although this is a bit academic as it is nearly always setup automatically as part of the hosting account setup. – w3dk Jan 24 '13 at 15:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.