HomeHTTPApache web serverRewrite domain.com to www.domain.com using htaccess in Apache

Rewrite domain.com to www.domain.com using htaccess in Apache

You may want all visitors to your site using www in front of your domain name (www.yourdomain.com) instead of just your domain name (yourdomain.com). By implementing a simple .htaccess RewriteRule, visitors to yourdomain.com will see the URL change in their browser as they are redirected to the correct URL.


To redirect a URL such as “http://yourdomain.com/images/logo.png” to “http://www.yourdomain.com/images/logo.png” use:

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

In the RewriteRule code, the R=permanent sets the type of redirection (permanent is 301; the default is temporary or 302). The L makes this the last Rule to use and stops rewrite processing.

Quinn McHenry
Quinn McHenry
Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
RELATED ARTICLES

9 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

Cleveland Furia on How to Clear Google Activity
Sid Wohlfarth on How to Clear Google Activity
Riley Magsayo on How to Clear Google Activity
สมัครรับเครดิตฟรี ทันที on How to improve Remote Desktop Protocol Performance
Neal Wangstad on How to Clear Google Activity
Slavcho Andov on How to install Ubuntu
Slavcho Andov on How to fix bad sectors
Sheraz Ali on How to Backup Android