Ridirect http://yoursite.com/index.php to http://yoursite.com

Sometimes we need to 301 redirect the index.php file to the domain root as they are canonical urls (ie, http://yoursite.com/index.php and http://yoursite.com are same).
It is only tricky for sites using index.php as the common entry with GET variables controlling the site . In that case we cannot redirect pages with any query string (anything after ? mark).

Following is the solution that’ll redirect any request to homepage of the site to the root url without redirecting any url that has query string.

RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} !(.+)
RewriteRule ^(.*)$ http://www.yoursite.com [L,R=301]

The first line checks if the requested file is “index.php”
Second line checks if there is any query string, if there is any query string, the rewrite will not work.
If the above 2 conditions are met, the url is redirected to the root url.


, , ,

Comments are closed.