Still writing old school php? At least hide your .php extension

Posted by ryan in php

Since almost everyone is now programming in some complex web development framework (mine being Symfony), the days of programming old-school php (read with pages like contact_us.php and about_us.php) are basically dead.

Still, there is a time and a place for such simple applications. Just today I wrote one, but I still wanted the cleanliness of having urls WITHOUT the .php extension. The solution, as pieced together via forums is to place the following into your .htaccess file:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^(.*)$ $1.php

http://example.com/test --> http://example.com/test.php
(meaning that the user sees just "test" in the browser, but sees the result of executing the test.php script)

Even better, if the actual file exists, then apache routes straight to that file. This means that you can still go directly to http://example.com/test.php. It also means that routing to your images, stylesheets and javascript won't be disturbed.

Thanks for the shares!
  • StumbleUpon
  • Sphinn
  • del.icio.us
  • Facebook
  • TwitThis
  • Google
  • Reddit
  • Digg
  • MisterWong