Htaccess – redirect domain to WWW

If you have a website for sure you have come to the point wondering how to redirect all visitors to the WWW part of the domain. This is small tip explaining in details the actual .htaccess redirect. Enough talking let’s see some code:

 

 

 

 
[cc escaped=”true” lang=”text”]Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC][/cc]

More details

FollowSymlinks – this option tells the server to follow Symbolic links. This are aliases or short cuts of existing files and/or directories on your server. By default this option should be enabled and you could skip this row but to prevent any 500 errors you’d better use it.

RewriteEngine on – turns on the actual URL rewriting engine that will fetch the request URL and change it according the following rules and conditions in the file.

RewriteCond  – This is back reference to  that will match our hostname/domain and if it starts with “domain.com” and it’s not a sub domain like “test.domain.com”, next row RewriteRule will be executed. Maybe you have spotted the flag at the end [NC] – it means no case, more thoroughly, our domain is matched in case insensitive way so “domain.com” and “DomAiN.com” are the same thing.

RewriteRule – That is the actual place where the magic happens. This command specifies a rule that will be executed upon request. It receives two parameters. The first one is a regular expression which fetch entire URL and replace it with the second parameter “http://www.domain.com/$1”. The number one tells the server that all following data after the main URL should be appended to the end of our new domain. This is taken from brackets in the first variable. You can reference as many parts as you want. Fast example is ([0-1]+)/([a-z]+) and $1 will contain all numbers and $2 will contain one or many characters. The flag at the end [R=301] defines a redirect also known as “Moved permanently” which will preserve your SEO ranking and will prevent search engines penalties.

Meta redirect/refresh

To do a server redirect you should have access to the server and you should have permission to create .htaccess file which is not always the case. In such situation you still can redirect your page by using meta tag called refresh.
[cc escaped=”true” lang=”html”]<meta http-equiv=”refresh” content=”2;url=www.yysource.com”/>[/cc]
You have to place this tag in the head of your page. The most important part is the content parameter. First thing, the number defines how long to wait before redirect the user. In our case 2 seconds followed by semi column and the link of the page we want to redirect.

JavaScript redirect

And finally we can use JavaScript but this is not the best approach considering some users may have disabled their JavaScript support in their browsers or the website is visited by search engine bot, they don’t have JavaScript so apparently won’t be redirected and may see wrong page or index wrong content. Anyway the JavaScript redirect is done as follows:
[cc escaped=”true” lang=”javascript”]<script type=”text/javascript”>
<!–
window.location = “http://www.google.com/”
//–>
</script>[/cc]

It's only fair to share...Buffer this pageShare on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Pin on Pinterest0Email this to someone
About

Just a guy with strong interest in PHP and Web technologies

Tagged with: , , , , ,
4 comments on “Htaccess – redirect domain to WWW
  1. hmmm nice 🙂 🙂 🙂

  2. magasin jouet says:

    Excellent post. I was checking constantly this blog and I am impressed! Very helpful info particularly the last part 🙂 I care for such information a lot. I was seeking this certain info for a very long time. Thank you and good luck.

  3. best carpet washer Victoria says:

    What’s up, I wish for to subscribe for this weblog to
    obtain latest updates, so where can i do it please
    help out.

  4. Constipation says:

    I blog often and I truly appreciate your information. This article has really
    peaked my interest. I’m going to book mark your blog and keep checking for
    new information about once a week. I opted in for your RSS feed too.

Leave a Reply

Your email address will not be published. Required fields are marked *

*