Home > Article > Backend Development > How to Hide PHP Extension in .htaccess: A Comprehensive Guide?
Hiding .php Extension in .htaccess: A Comprehensive Solution
In web development, it's often desirable to hide the .php file extension to enhance the URL's aesthetic appeal and prevent malicious activity. However, achieving this can sometimes pose a challenge.
Implementing the Solution
To effectively hide the .php extension using .htaccess, consider the following code:
RewriteEngine On # Unless directory, remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)/$ http://example.com/folder/ [R=301,L] # Redirect external .php requests to extensionless URL RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/ RewriteRule ^(.+)\.php$ http://example.com/folder/ [R=301,L] # Resolve .php file for extensionless PHP URLs RewriteRule ^([^/.]+)$ .php [L]
Explanation
The above is the detailed content of How to Hide PHP Extension in .htaccess: A Comprehensive Guide?. For more information, please follow other related articles on the PHP Chinese website!