I want to write URL rewrite for index.php as a request path and it should be redirected to the home page


I think the best way to implement this is to use server configuration, for example if you use apache, the following rewrite command should do it (you might be able to put this also in .htaccess file)

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

| improve this answer | |
  • This is for removing index.php from website URL. I have done that already. But when I insert index.php in the URL it should be redirected to the home page. For that I want to write rewrite rule, How can I do that?? – Smita Kagwade Feb 19 '19 at 10:16
  • My code does a redirect, that's what R=301 stands for: it does a redirect with HTTP Status code 301 – HelgeB Feb 19 '19 at 10:20
  • Yes, I want to redirect. Got the answer. Thanks – Smita Kagwade Feb 19 '19 at 10:25

Add This to your .htaccess file

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

This worked for me. Thanks

| improve this answer | |
  • This rule does more than just redirecting index.php to the homepage, it removes every index.php occurence from a request and redirects to the remaining URL. But anyway if it is what you need it's good for you. – HelgeB Feb 19 '19 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.