Home >Backend Development >PHP Tutorial >How to Hide .php File Extensions with .htaccess?
Hiding .php File Extensions in .htaccess
Users often seek to conceal the .php extension from their website's URLs. The .htaccess file, a powerful configuration file, aids in this endeavor.
One unsuccessful attempt at this involved the following code:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^folder/([a-zA-Z_\-0-9]+)/?$ /folder/.php </IfModule>
Solution
A more effective approach utilizes 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]
This code:
The above is the detailed content of How to Hide .php File Extensions with .htaccess?. For more information, please follow other related articles on the PHP Chinese website!