Remove index.php From URL By URL Rewriting Or htaccess Rule To Remove Index.php

Hello Friends, Today i was working on one project in which I have to remove index.php from URL because http://example.com/index.php and http://example.com/ both are same but technically google consider it different URLs, There is one other method by which we can tell google about one best URL which is canonical tag. But I want to do it via URL rewriting and remove index.php from URL means when anyone enters http://example.com/index.php it redirects to http://example.com/ , I have searched alot and finally I found it So i thought it would be great if I share it on this site, So it can be helpful to someone.

Below is the rule of .htaccess by which you can remove index.php from URL means if anyone enters http://example.com/index.php it redirects to http://example.com/.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /([^/]+/)*index.php HTTP/
RewriteRule ^(([^/]+/)*)index.php$ http://www.%{HTTP_HOST}/ [R=301,NS,L]

Above are the two rules by which you can redirect index.php to root directory. If you have any problems in these rules then you can contact me or post it into comments.

  • Jamie Iomo

    Hello

    Thanks for sharing, I have digged this post

Subscribe to PHP Freelancer

Enter your email address: