how to hide php extension in url using .htaccess

January 4, 2017 52.1k views
PHP Ubuntu 16.04

i want to be able to hide php extension in url so that if i got to login it will work the same way as if i was going to login.php

6 Answers

Try my version:

# Run Php without filename extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

# Return 404 if original request is .php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

You can use the following.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Basically this says if the File in the URL can’t be found on the server then in the backend load it again using .php. This will basically make the server take a /file request and because there is no /file on the server it will load /file.php instead. The user wont see the switch over but can still load both ways.

  • doesn’t seem to work for me

    • Trying using this version

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

      You may also need to set mutliviews if so add this to the top of the htaccess file.

      Options +MultiViews

Thanks for sharing..
Working fine.....

Very very thanks…
code working

How to add 404 error page when extension restricted .php and .html both

If you looking for complete solution with explanation then i will recommended to Read this:
How to Hide PHP Extenstion Step By Step

You will get your answer 100%.

Have another answer? Share your knowledge.