Search code examples
regex.htaccesshttp-redirectmod-rewriteurl-rewriting

redirect and remove slug if found anywhere using .htaccess


I want to remove index.php/

example.com/index.php/apple
example.com/fruits/index.php/apple
example.com/fuits/apple/index.php/ripen
example.com/index.php

in any of the above cases, I need the url to be redirected by removing index.php/

I tried

RewriteEngine on 
RewriteBase / 
RewriteRule ^index.php/(.*) /$1 [R=301,L]

But it only works for the first case from the given urls above.


Solution

  • With your shown samples, please try following htaccess rules file. Make sure to keep your file in root.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##Doing external redirect for index.php here.
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php/?$ /? [R=301,L]
    ##Doing external redirect to remove index.php here.
    RewriteRule ^(.*/)?index\.php/(.*) /$1$2? [R=301,L]
    ##Handling home page with internal rewrite here.
    RewriteRule ^/?$ index.php [L]