2015/04/09

How to redirect non-www URL to www in Apache



If both www and non www domain of your website (http://example.com and http://www.example.com) serve the same website, and you want people going to http://example.com to automatically be redirected to http://www.example.com, there's a few way that you can do so with Apache




.htaccess




First of all, make sure mod_rewrite is installed and enabled for your Apache installation. Once it is, add a file named .htaccess in your website's root directory and add the following lines;
RewriteEngine On
RewriteCond %{HTTP_HOST} ^!example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


Virtualhost




You can also change your website's Virtualhost setting for the redirect.
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>

You'll need to restart Apache for this method to work


ليست هناك تعليقات:

إرسال تعليق