views:

88

answers:

6

Hi, I've created a PHP pagination system, but I want to hide the _GET variable in the address bar.

At the moment mine looks like this: http://example.com/images.php?page=1

But I've seen a few site that have http://example.com/images/1/.

Just wondering how they go about this? Can anyone point me in the right direction? Cheers

+1  A: 

That's called PATH_INFO

The server must support it and enable it.

If it does, then you may access it using $_SERVER["PATH_INFO"]

Lo'oris
That isn't what PHP_INFO is... this is called mod_rewriting.
lol didn't even have the time to edit it :V
Lo'oris
Delete this answer otherwise you'll regret it.
zaf
+1 `PATH_INFO` is a perfectly valid way of rewriting URLs without the help of mod_rewrite with the limitation that the URL will have to contain the PHP file name: `www.example.com/images.php/1/` .
Pekka
@zaf: I'll regret what, rotfl? I misspelled `PHP_INFO` instead of `PATH_INFO`. Using `PATH_INFO` works and does *exactly* what he wants.
Lo'oris
@Lo'oris Ah you've edited your answer! Still, it won't work because if you look at the question closely you'll note that its '/images/' not '/images.php/' meaning that PATH_INFO won't have a chance since the webserver has already done a 404.
zaf
Yes, as Pekka pointed out it requires you to write the extension too, so if it's really a requirement not to do it, then this is not an option. Still, this might have sufficed for him. I don't understand what you mean about the 404.
Lo'oris
+7  A: 

You will need support from your web server to get the requests right and rewrite the requests of /images/1/ internally to /images.php?page=1. Most web server have a module or extension to support such behavior like Apache’s mod_rewrite, lighttpd’s mod_rewrite, ISAPI Rewrite module for ISS, etc.

Gumbo
thanks, knew it could be done, i just didn't know what to search for to get me started.
michael
@micheal: This technique is called URL/URI rewriting.
Gumbo
A: 

Most frameworks take care of it, but you need to configure your web server to handle it. Here are instructions from CodeIgniter on setting up apache htacces.

Ian
A: 

If you're using apache then you can use mod_rewrite to do this kind of magic: http://en.wikipedia.org/wiki/Rewrite_engine

zaf
A: 

If your server is Apache, you can create a file called .htaccess (it has no filename, only an extension - it's worth giving it a name on your local machine because some file systems hide files with no name) and learn how to use RewriteRules

It's very easy to use for simple rewrites like you want.

A sample .htaccess file:

RewriteEngine On

#Translate http://address.com/images/1/ to http://address.com/images.php?page=1
RewriteRule ^images/(.*)$ / images.php5?page=$1
This rule won’t work in the .htaccess file.
Gumbo
A: 

Usually all requests are redirected (their URL is internally rewrited) to single PHP script using Apache mod_rewrite and RewriteEngine, then (in that script) the URL is examined and handler matching this URL (if there are more handlers) with corresponding arguments is called. Some frameworks in PHP, usually MVC ones, are entirely built on one single index.php to which all requests are "redirected" (URL-rewrited).

If you need just to solve the problem with ?page=, try rewriting URL /images/1/ to /images.php?page=1 (as Gumbo suggests) with this in Apache configuration (the .htaccess file for example):

RewriteEngine on
RewriteRule ^images/([0-9]+)/?$ images.php?page=$1 [L]
Messa