how to hide php extension in url using .htaccess

January 4, 2017 5.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

3 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.....

Have another answer? Share your knowledge.