How to remove index.php from URL path except if the path is /index.php/

Viewed 21

I need to remove index.php from inner pages so that the URL path like /index.php/path would be just /path. But at that if the path is /index.php/ then not to remove index.php and leave everything as is. I try the following lines in my .htaccess:

RewriteCond %{REQUEST_URI} !^.*/index.php/$ [NC]
RewriteRule /index.php/(.+)$ /$1

But they remove index.php always including when the path is /index.php/

1 Answers

You can use these 2 rules in your site root .htaccess:

RewriteEngine On

# external redirect to remove /index.php
RewriteCond %{THE_REQUEST} \s/+index\.php/(\S+) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite to /index.php/<uri>
RewriteRule .+ index.php/$0 [L]
Related