How to Properly Redirect Your Domain With or Without www ?
Proper redirects are important. Failure to use them will mean search engines such as Google
will either see your site twice, or, they might not see it at all
either of which can dramatically reduce your search engine ranking.
For SEO these are extremely important as Google may penalize you for posting the same content in multiple websites and, if not done properly, a www redirect will in fact look to Google as if you have 2 websites, one with www and one without.
Fear not though, if you use a Linux based web host (or any host using the Apache web server) the fix is easy.
Step 1:
Figure out if you want your website address to display with the www or without it.Visitors will still be able to use both, but you want to settle on 1 for SEO purposes. From a technical point of view it does not matter which you choose only that you choose one. If you have printed material and/or many links using it one way or the other settle on that one.
Step 2:
Open up the .htaccess file in the root (top folder of your website where your index file lives) or create the file .htaccess if it doesn’t already exist.Step 3:
If you want to display the www enter the following lines:
RewriteEngine On |
RewriteCond %{HTTP_HOST} ^your-domain\.com |
RewriteRule (.*) http://www.your-domain.com/$1 [R=301,L] |
If you do NOT want to display the www enter the following lines:
RewriteEngine On |
RewriteCond %{HTTP_HOST} ^www\.your-domain\.com |
RewriteRule (.*) http://your-domain.com/$1 [R=301,L] |
Note the backslash “” before the period(s) on the 2nd line of each block of code. This is very important and your redirect will not work without it.
How does this work? It makes use of an Apache module called mod_rewrite which is first invoked using the “RewriteEngine On” line. Next, it looks for a condition in which the domain name is specified as listed on the 2nd line. Finally, it sends the user to the new domain followed by whatever page they were trying to get (the $1 is anything after the slash following the domain name in the URL). It also makes it a permanent redirect with R=301 and tells the server it is the last rule to process in its chain with the L.
Step 4:
Save the file and test it by going to your website with and without the www making sure you see the desired behavior.Step 5:
Test it externally with a site like pearanalytics which will tell you if it is not redirecting correctly.What If htaccess file doesn't work? Enabling .htaccess file to rewrite path (not working)
The best way is :
Edit or creat config file/etc/apache2/conf-available/httpd.conf
Add
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Enable the config file
sudo a2enconf httpd
Restart or reload Apache
sudo service apache2 restart
or
sudo service apache2 reload
It's done!
Alternatively,
New apache version has change in some way. If your apache version is 2.4 then you have to go to
/etc/apache2/
. There will be a file named apache2.conf
. You have to edit that one(you should have root permission). Change directory text like this
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Now restart apache.service apache2 reload
Hope it works.
No comments:
Post a Comment