Hezerac Hezerac - 2 months ago 7
PHP Question

.htaccess remove index.php from subdir redirects all dynamic pages

Trying to remove index.php from and just resolve to the subdirectory name which works, however after making the change to .htaccess file all the other pages redirect to the subdirectory.

For example:

Changed:

site.com/subdir/index.php
to
site.com/subdir/

but then
site.com/subdir/page?id=4
resolves to
site.com/subdir/




.htaccess:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]


RewriteBase /subdir/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]


How to write this rule so that only index.php resolves to /subdir/ and not the other pages within that directory?

Answer

You can use these rules in site root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*/)index\.php[?\s] [NC]
RewriteRule ^ %1 [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ subdir/index.php [L]