Hiding file in address bar
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
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.
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"]
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.
If you're using apache then you can use mod_rewrite to do this kind of magic: http://en.wikipedia.org/wiki/Rewrite_engine
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]
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
精彩评论