Jump to Navigation

275 - Apache 301 How to redirect

Search engines consider http://mydomain.com and http://www.mydomain.com different websites.
As a result, if your website has been linked to from other websites using a mix of the two URLs you are effectively
splitting the potential benefit of valuable link popularity.

Using a 301 redirect on the “non-www” version of the URL, which is essentially a “permanent” redirect in server talk,
you can effectively consolidate all of your link popularity to a single URL. This consolidation will serve to increase your
website’s chances of obtaining and maintaining top rankings.

1. Configure rewite_module in httpd.conf

# vi /etc/httpd/conf/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

If you are using apache 2.x or latest, you do not need the line.

httpd: Syntax error on line 35 of /usr/local/apache/conf/httpd.conf:
module rewrite_module is built-in and can't be loaded

Once the line is uncommented in httpd.conf, save the file and restart the server then proceed with the following steps.

2. Edit .htaccess in the web root documents

Add the following lines in .htaccess

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

or

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

or

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

3. HTTP / HTTPS Header Check

You can check the redirect. (302 is temporally)

URL   http://web-sniffer.net/

You could be getting :

Status: HTTP/1.1 301 Moved Permanently

4. Directory options

If you have no change on the URL, check the directive on Directory in httpd.conf.

<Directory />
 Options FollowSymLinks
 AllowOverride None
</Directory>

* AllowOverride None --> All

5. Domain Registration on Names Server

MS-IE does not change the URL unless there is no registration of NON WWW domain name in
your names server. Others(firefox, safari, Opera and so on) could change the URL to www.*
without names server registration. MS-IE launches the Bing to search the domain.

You have to register the following domain names in the global names server.

* www.myserver.com
* myserver.com

6. Debug in httpd.conf

Nothing happens after the change, you could debug the problem with the log. Please edit httpd.conf like the following.
After change, you need to restart Apache Server.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteLog "/hoge/fugo.log"
RewriteLogLevel 9
<IfModule>

7. Redirect by Virtual Server settings

You need to configure two hosts as the followings.
It redirects NON WWW to WWW.

<VirtualHost 192.168.1.10>
ServerName www.myserver.com
DocumentRoot /home/user01/html

<Directory "/home/user01/html">
allow from all
Options +Indexes
AllowOverride all
</Directory>

</VirtualHost>

<VirtualHost 192.168.1.10>
ServerName myserver.com
Redirect 301 / http://www.myserver.com/
</VirtualHost>

 



Main menu 2

Story | by Dr. Radut