19

I want something to put in my .htaccess file that will hide the .php file extension for my php files, so going to www.example.com/dir/somepage would show them www.example.com/dir/somepage.php.

Is there any working solution for this? My site uses HTTPS, if that matters at all.

This is my .htaccess at the moment:

RewriteEngine On

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

ErrorDocument 404 /error/404.php

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Markum
  • 3,919
  • 8
  • 26
  • 30

4 Answers4

52

Use this code in your .htaccess under DOCUMENT_ROOT:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

It should be noted that this will also effect all HTTP requests, including POST, which will subsequently effect all requests of this kind to fall under this redirection and may potentially cause such requests to stop working.

To resolve this, you can add an exception into the first RewriteRule to ignore POST requests so the rule is not allowed to them.

# To externally redirect /dir/foo.php to /dir/foo excluding POST requests
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    how can you exclude pages? This seems to be interfering with my login script so i would like to exclude that page from these rewrites – ChristopherStrydom Aug 08 '14 at 14:02
  • Thanks will give it a try. What i think is happening is when the page is being reloaded without the extension it is loosing the variables. is there a way to fix this? Its as if its logging in twice.... I think... once with the email and pass and then again without. so in all i cant login – ChristopherStrydom Aug 08 '14 at 14:30
  • I tired `RewriteCond %{REQUEST_URI} !^/(login.php)` but it doesn't seem to be doing anything? – ChristopherStrydom Aug 08 '14 at 14:37
  • After using this as my .htaccess, all my login will fail. It will keep direct me back to the login page. Any idea why this happen? – Arios Jan 26 '17 at 07:05
  • 4
    @Arios: Add `RewriteCond %{REQUEST_METHOD} !POST` condition in first redirect rule to avoid POST requests getting redirected. – anubhava Jan 26 '17 at 10:17
  • 1
    @anubhava you saved my life – Fakhamatia Mar 31 '20 at 19:22
  • 1
    @anubhava 3 years later, you have also saved my life – Skully Aug 05 '20 at 04:54
  • @Skully: Thanks so much for your kind words and important edit in answer. – anubhava Aug 05 '20 at 07:59
5

Remove php extension

You can use the code in /.htaccess :

RewriteEngine on


RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [NC,L]

With the code above, you will be able to access /file.php as /file

Remove html extension

RewriteEngine on


RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [NC,L]

Note : 'RewriteEngine on' directive should be used once at top per htaccess, so if you want to combine these 2 rules just remove the line from second rule. You can also remove any other extensions of your choice, just replace the .php with them on the code.

Happy htaccessing!

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
3
<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f       # if the requested URL is not a file that exists
RewriteCond %{REQUEST_FILENAME} !-d       # and it isn't a directory that exists either
RewriteCond %{REQUEST_FILENAME}\.php  -f  # but when you put ".php" on the end it is a file that exists
RewriteRule ^(.+)$ $1\.php [QSA]          # then serve that file (maintaining the query string)

</IfModule>

Oddly, this code gave me HTTP 500 errors when tried in XAMPP in Windows. Removing the comments fixed it. So did moving the comments so they were on their own lines, instead of on the same lines as instructions.

TRiG
  • 10,148
  • 7
  • 57
  • 107
0

Try this:

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]Try:

Reference:

How to hide .php extension in .htaccess

Community
  • 1
  • 1
Wes Crow
  • 2,971
  • 20
  • 24