How to Remove .php File Extension While Allowing Access to Folders in PHP 8.2?

hitjethva

New member
I’ve configured .htaccess to remove the .php file extension, but it causes issues when accessing folders (e.g., https://code2devops.com/devops/ since it assumes any path without an extension refers to a .php file. Is there a way to achieve this setup so that files without an extension default to .php, while folders remain accessible as-is?
 
HI ! try this

Code:
RewriteEngine On
RewriteBase /

# Prevent direct access to .php files
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^(.*)\.php$ /$1 [R=301,L]

# Internally rewrite extensionless URLs to .php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

# Ensure directories work correctly
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ $1/ [L]

This configuration should allow you to remove the .php file extension from your URLs while still allowing access to directories as intended.
 
Last edited:
Back
Top