Apache: 301 redirect non-WWW to WWW URLs

First check if mod_rewrite module is enabled. If not, check this guide how to do it.

Edit .htaccess file from the document root of your website.

Add this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

If you need the opposite - redirect WWW URL to a non-WWW add this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Save changes.

Navigate to your web site in a browser without WWW. If you did it all correct WWW will be added in the address bar automatically.







Got a comment?

captcha =

Categories

  1. System (20)
    1. FreeBSD (5)
    2. Linux (9)
  2. Email (2)
  3. DNS (2)
  4. Databases (1)
  5. WebServer (27)
 
Copyright © 2012-2015 HowToUnix - *nix Howtos and Tutorials
All Rights Reserved.