Skip to content
Advertisement

How can I hide php file extension using htaccess?

RewriteRule ^site/index$ site/index.php [NC,L]

this line does hide the extension successfully. Now I do not want the page with the extension to be accessible; if one tries to access that page with the extension, it must show an error. Only the one that has the extension hidden that must be accessible.

Advertisement

Answer

@MrWhite gave an answer that showed how to throw an error message like you asked, but why would you want to throw an error message instead of just redirecting example.com/page.php to example.com/page? Here is the .htaccess code for that:

RewriteEngine on
RewriteRule ^(.+).php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

This makes sure that both /page and /page.php go to the same url (/page) and serve the same page/file (/page.php)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement