How To Safely Redirect /index.php To / Using .htaccess / Apache

By Angsuman Chakraborty, Gaea News Network
Saturday, June 28, 2008

Any PHP based software like WordPress, Mambo, Joomla, Drupal, MODx etc. will serve pages both from http://your-site.com as well as http://your-site.com/index.php. This creates duplicate content for search engines, which may then decide to penalize your site for duplicate content. You can safely prevent duplicate content with the following apache code / directive added to your .htaccess file (or in httpd.conf):

RewriteEngine On
#Redirect plain index.php to home page without index.php
RewriteCond %{IS_SUBREQ} false
RewriteRule ^/index\.php$ http://blog.taragana.com [R=301,L]

Replace http://blog.taragana.com with the url of your own site.

What it does is redirect (301 - permanent redirect) all access to /index.php to /
However it does not redirect for internal requests. This is very important because when you are fetching http://blog.taragana.com, Apache actually creates an internal redirect to http://blog.taragana.com/index.php

The RewriteCond above ensures that only external accesses (from browsers, search engine bots etc) are only redirected to the canonical url of the site.

Also it doesn’t redirect requests like this http://blog.taragana.com/index.php/archive/java-application-security-through-static-analysis/, which contains index.php as part of the url. Only the exact url http://blog.taragana.com/index.php is redirected to http://blog.taragana.com

Discussion
December 22, 2008: 4:13 pm

Hi,
I’m in a “extremely incompetent hosting company” situation :) that don’t permit me to use my website without DNS.
Now I’ve put a webserver APACHE on my PC always on with static IP and I’ve forwarded the DNS * and @ to my IP to get the request without www for my website and “redraw” them with http://WWW.
I have some problems in doing this.
For now I’ve tried this:

$server = $_SERVER['SERVER_NAME']; header(’Location: http://www.’ . $server);

but doesen’t work with every page (i.e. http://website.ext/* for every possible string in *).
Can you suggest me how to fix this problem?
I repeat, I have an apache webserver so I can also modify settings in apache if is not possible to do this only with PHP.
Thanks so much.
PS: It can be better if this fix works independent by domain name, so I can help people with same problem.
For knowing, the hosting company is TopHost.it
I remember you that my website is hosted OUTSIDE my pc! SO I need only a REDIRECT

December 22, 2008: 4:13 pm

Hi,
I’m in a “extremely incompetent hosting company” situation :) that don’t permit me to use my website without DNS.
Now I’ve put a webserver APACHE on my PC always on with static IP and I’ve forwarded the DNS * and @ to my IP to get the request without www for my website and “redraw” them with http://WWW.
I have some problems in doing this.
For now I’ve tried this:
$server = $_SERVER['SERVER_NAME']; header(’Location: http://www.’ . $server);
but doesen’t work with every page (i.e. http://website.ext/* for every possible string in *).
Can you suggest me how to fix this problem?
I repeat, I have an apache webserver so I can also modify settings in apache if is not possible to do this only with PHP.
Thanks so much.
PS: It can be better if this fix works independent by domain name, so I can help people with same problem.
For knowing, the hosting company is TopHost.it
I remember you that my website is hosted OUTSIDE my pc! SO I need only a REDIRECT

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :