Skip to content

Instantly share code, notes, and snippets.

@xxjapp
Last active August 3, 2017 07:58
Show Gist options
  • Save xxjapp/fd914df0bef717d80e93e545cdde485f to your computer and use it in GitHub Desktop.
Save xxjapp/fd914df0bef717d80e93e545cdde485f to your computer and use it in GitHub Desktop.
hide php extension in url
http://php.net/manual/en/security.hiding.php
So far I haven't seen a working rewriter of /foo/bar into /foo/bar.php, so I created my own. It does work in top-level directory AND subdirectories and it doesn't need hardcoding the RewriteBase.
.htaccess
RewriteEngine on
# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment