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

Home -> Internet -> HTTP -> Apache

13757 views

From the computer of: qmchenry (337 recipes)
Created: Dec 27, 2003


0 comments:
View all comments

Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

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.

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  Password protect a web directory with .htaccess
  Restart Apache without closing open connections
  HowTo: Reboot the Apache Server Safely
  Test the Apache configuration file httpd.conf
  Stop Apache server processes

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.