301 Permanent Redirect NON WWW to WWW and viceversa on Apache2

Valic —  December 3, 2011 — 1 Comment

Apache2 is one of the most widely used HTTP server has a lot of features and can be customized in many ways.

In fact, to this web server, there are basically two ways you can do this, using .htaccess directives or using a file httpd.conf. .

Some people prefer to use. htaccess files, since they reside in the website and can be modified by the site owner, which is not the case for the daemon configuration file:

 

Permanent redirect  NON WWW to WWW using apache2 server configuration:
ServerName debian-tutorials.com
RedirectMatch permanent ^/(.*) http://www.debian-tutorials.com/$1
DocumentRoot /path/to/your/site/files/
ServerName www.debian-tutorials.com
Permanent redirect from WWW to NON WWW using apache2 server configuration:
ServerName www.debian-tutorials.com
RedirectMatch permanent ^/(.*) http://debian-tutorials.com/$1
DocumentRoot /path/to/your/site/files/
ServerName debian-tutorials.com
Permanent redirect from NON WWW to WWW  using htaccess.:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^debian-tutorials\.com$ [NC]
RewriteRule ^(.*)$ http://www.debian-tutorials.com/$1 [R=301,L]

Permanent redirect from WWW  to NON WWW  using htaccess.:

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

Valic

Posts Twitter Facebook

Editor in Chief at Debian-Tutorials, Linux enthusiast.

One response to 301 Permanent Redirect NON WWW to WWW and viceversa on Apache2

  1. Hmm.. using RedirectMatch for such simple redirects seems a little overkill. You could do the same with Redirect to avoid the regular expression matching to happen (since anyway you want to simple redirect to the same thing under www):

    Redirect permanent / http://www.debian-tutorials.com/

Leave a Reply