Remove .php extension with PHP

Tobias picture Tobias · Aug 26, 2009 · Viewed 10k times · Source

I could use some help here. I'm using this to fix my URL but I can't figure out how to remove the .php extension. The URL looks like this now: http://mydomain.com/page.php/foo/123/bar/456

function decode_URL_parameters() {
   $path = @$_SERVER['PATH_INFO'];
   $url_array=explode('/',$path);

   array_shift($url_array);

   while ($url_array) {
      $_GET[$url_array[0]] = $url_array[1];
      array_shift($url_array);
      array_shift($url_array);
   }
}

Any ideas?

/Tobias

Answer

Duncan Beevers picture Duncan Beevers · Aug 27, 2009

If you're serving via Apache, you'll want to look at mod_rewrite.

Using mod_rewrite, you can modify how URLs are mapped to your application's actual end-points. For your example, you'll want something like this:

RewriteEngine on 
RewriteRule ^/?page/(.*)$ page.php/$1 [L]

This page has some other simple examples.