How to Redirect Index PHP to Root or Home Page

0 14

How to redirect index.php to home page or root of the origin domain
Most users may find that “example.com” and “example.com/index.php” may show the same page, its bad for SEO (Search Engine Optimization) and results in duplicate content, this guide will show you how to redirect index.php to (/) root or home-page of your domain, it doesn’t have to be index.php it can be home.php or with a different name

Add the following code in your .htaccess file with necessary changes

Replace example.com with your website url

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?example.com [NC]
RewriteCond %{THE_REQUEST} s/index.php[s?] [NC]
RewriteRule ^index.php$ /? [NC,R=301,L]

So now the explanation

RewriteEngine On

This line is added because we are using mod_rewrite apache module here, this only has to be added once and add it as the first rule of .htaccess

RewriteBase /

As for the RewriteBase, you only need that if you have relative URI’s as targets, or if you want your rules to be portable (thus you only need to change the base if you move rules from one directory to another). For example:

Don’t need RewriteBase for these rules:

RewriteRule ^login.php /login.php? [L]
RewriteRule ^.*$ - [F,L]

Because /login.php isn’t a relative URI, so it’s understood that you’re literally referring to http://domain.com/login.php and the – just means pass-through so the base is ignored anyways.

An instance where you may want to use RewriteBase:

RewriteRule ^dir/(.*)$ dir2/$1 [L,R=301]

Without “RewriteBase /” apache will probably guess incorrectly whether “dir2/$1” is a file or URI path and redirect you to the wrong place, with RewriteBase / then it’ll redirect correctly.

Replace example.com with your url

RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?example.com [NC]

this argument simply ignores rewriting when the clicks are made from origin (example.com) the website can be ssl or non-ssl (https or non-https) which prevent the rewrite rule from breaking the internal links of the site

RewriteRule ^index.php$ /? [NC,R=301,L]

The final rewrite rule

This will only redirect if the url matches example.com/index.php to example.com/  which prevents the mod_rewrite from breaking up page links
Additionally in some cases and forums like Invision Power Board – IPB
The Urls have to parse through index.php and some urls patterns should not be rewritten

Here is an example of pattern this pattern ignores any urls patterns like the following

app=something/something-more/some-times-nothing
app=(here it can be anything)
RewriteCond %{QUERY_STRING}    !^app=(.*)$    [NC]

# This condition is most useful with Invision Power Board Forums / IPS Suite modify the condition as you need
Finally in some cases you may find that
example.com/index.php/ may not be redirected to the root (/) the home page
Add this simple redirect rule to your .htaccess t0 fix it

Redirect 301 /index.php/ /

Leave A Reply