Archive for October, 2011

Worpress

Posted: October 14, 2011 in Uncategorized
Tags:

test

Hide PHP extension in URL using .htaccess

Posted: October 1, 2011 in Php

What?

Hide the .php extension of your PHP files in the URL of your site address.
Instead of:
http://www.example.com/page.php
Visitors to your site will see:
http://www.example.com/page/

Why?

There are actually quite a few reasons to do this.

  • Make your URLs cleaner and easier to remember for visitors
  • Make dynamic pages appear static for SEO
  • Security by obscurity (albeit very weakly so)—visitors cannot tell as easily that you are using PHP
  • For fun (yay!)

How?

There a few ways you can do this.

Method 1: example.com/test => example.com/test.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Source: newmediarts.blogspot.com/
With this rewrite, http://www.example.com/test.php is the same as http://www.example.com/test but NOT http://www.example.com/test/
GET variables can still be pushed through: http://www.example.com/test.php?id=5 is the same as http://www.example.com/test?id=5

Method 2: example.com/test/ => example.com/test.php

RewriteEngine on
RewriteRule ^(.*)/$ $1.php

This is a slight variation on the first method, except now the php file looks just like a directory. You can still access files within the directory. Parameters get passed correctly as well, though it looks a little goofy.
http://www.example.com/test/ => http://www.example.com/test.php
http://www.example.com/test/?id=5&code=4 => http://www.example.com/test.php?id=5&code=4
http://www.example.com/test/anotherfile.html => http://www.example.com/test/anotherfile.html (only directs paramters if it starts with ?).

Method 3: example.com/test/id/5/ => example.com/test.php?id=5

This method allows the user to pass on one parameter hidden as directory folders.

RewriteEngine on
RewriteRule (.*)/(.*)/(.*)/$ /$1.php?$2=$3

http://www.example.com/test/ => http://www.example.com/test.php
http://www.example.com/test/id/5/ => http://www.example.com/test.php?id=5
http://www.example.com/test/id/5/code/4/ => does not work

Method 4: example.com/test/id/5/code/4/ => example.com/test.php?id=5&code=4

This method allows the user to pass on exactly two parameter hidden as directory folders.

RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/$ /$1.php?$2=$3&$4=$5

http://www.example.com/test/ => http://www.example.com/test.php
http://www.example.com/test/id/5/ => does not work
http://www.example.com/test/id/5/code/4/ => http://www.example.com/test.php?id=5&code=4
Obviously this has some shortcoming. If anyone knows of a more functional script, please share it.

Help! It’s not working!

If you are in charge of configuring the Apache server, you may need to enable .htaccess in the config file of Apache. See tildemark.com’s short guide.
If you are having trouble creating an .htaccess file (Windows will not allow you to rename a file to .htaccess because it has no name, just an extension), start up notepad and save a file as ".htaccess" (including the quotes).
Now you can copy it wherever you need.

More reading

General tips and tricks with .htaccess corz.org/
Module Rewrite & URL rewriting guide widexl.com/