Posts Tagged ‘redirection

01
Jun
09

Redirecting index.php to site domain easily

It can be done with mod_rewrite in .htaccess like this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

THE_REQUEST is the original request from the client (browser or robot). The RewriteCond will only allow the RewriteRule to be invoked if the client actually requested index.php, and not if the current (internal) request is for index.php only as a result of mod_dir’s DirectoryIndex internal URL rewrite. While the rule pattern appears to be redundant, it improves the code efficiency because RewriteConds are only processed if the RewriteRule pattern matches.

The first directive may or may not be needed. It may cause a server error if it is needed but missing, or if it not needed but present. This depends on your server, so you may have to test to find out. The first two directives won’t be needed if you already have working rewriterules in your .htaccess file.