3

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

Tobias
  • 35
  • 1
  • 2
  • 4
  • You're trying to create what are known as "Pretty URLs", and they've been discussed on SO before. [This answer](http://stackoverflow.com/questions/128796/pretty-urls-for-search-pages/128882#128882) should have what you're looking for. – s4y Aug 26 '09 at 21:56
  • You can't do this using PHP alone, you need to dig into the Apache module mod_rewrite. – Alix Axel Aug 26 '09 at 22:01

4 Answers4

5

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.

Duncan Beevers
  • 1,830
  • 11
  • 17
3

You probably configured your webserver to let php handle all requests for files having the .php extension. Let's assume it's an apache. Then there's something like

AddType application/x-httpd-php .php .php5 .php4 .php3 .phtml .phpt

or

<FilesMatch \.php$>
  SetHandler application/x-httpd-php
</FilesMatch>

in one of the configuration files. The second one tells the apache that all files that match the mask .php$ should be handled by the module/handler that registered for the type application/x-httpd-php (php).
But you don't have to limit it to .php files. You can use any file mask you want. Or another directive like e.g. <Directory>.
If all files in a specific directory (and maybe its subdirectories) are php scripts you can even put an .htaccess file in that directory with

ForceType application/x-httpd-php

and the apache will treat any file as php script. ForceType overrides any previous handler settings.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
2

Under Apache, put a .htaccess file in the directory which contains the page.php file.

If the page.php files contains in the root dir (ie /page.php), then simply add this in your .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /page.php?/$1 [L]

If however, the page.php files contains in lets say /a_folder/another_folder/page.php

Then you will need to put this into your .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /a_folder/another_folder/page.php?/$1 [L]

And so on.

What does this is, it simply removes page.php from the URL. So in your case:

Instead of: http://mydomain.com/page.php/foo/123/bar/456 You can use: htp://mydomain.com/foo/123/bar/456

Have fun.

Waleed Amjad
  • 6,716
  • 4
  • 35
  • 35
0

thank you for all answers but if I use mod_rewrite then I haft to make new rules (and many) everytime I create a new page. And there is a lot of them :)

Perhaps if I redirect all urls to my index.php with .htaccess and then fix my URL there, like wordpress:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Tobias
  • 35
  • 1
  • 2
  • 4