Fede E.
Fede E.

Reputation: 1918

htaccess rewrite rules and removing php file extension

I have the following code in my htaccess file to remove the PHP extensions:

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

However, now I added a file in /directory/index.php and I can only access it using /directory/index instead of using just /directory/ (which now throws a 404 not found).

Upvotes: 2

Views: 305

Answers (1)

anubhava
anubhava

Reputation: 786289

Keep your rules like this in your root .htaccess:

DirectoryIndex index.php

RewriteEngine On
RewriteBase /

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Upvotes: 1

Related Questions