skip to Main Content

How to remove index.php from home page URL

example.com/index.php

to

example.com

and

example.com/directory/index.php

to

example.com/directory

I read many posts on stackoverflow and when I’m using those codes then if I go to an invalid URL (like – example.com/ahjgbhjkllghfd) then it will not showing 404 response. it shows response 200 and main page content on the invalid URL.

{ sorry for bad English 🙂 }

2

Answers


  1. Could you please try following, have written on mobile couldn’t test it should work I believe.

    RewriteEngine ON
    RewriteRule ^(.*/?)index.php/?$ /$1 [R=301,NC,L]
    
    Login or Signup to reply.
  2. If you need 404 error response then try adding ErrorDocument line

    ErrorDocument 404 /error.php
    

    It will show /error.php file when /ahjgbhjkllghfd is accessed.

    All together might be this

    RewriteEngine on
    RewriteRule ^(.*?)index.php$ /$1 [R=301,NC]
    
    ErrorDocument 404 /error.php
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search