I come from a Java background and with any servlets-based technology, it's trivial to map a range of URLs (eg /reports/, /secure/.do) to a specified servlet. Now I'm less familiar with PHP but I haven't yet seen anything that does quite the same thing with PHP (or mod_php). It's entirely possible that I'm missing something simple.
How do you do this in PHP?
One of the reasons I want to do this is "one use" URLs. Now this can sorta be done with GET parameters (like an MD5 hash token) but I'm interested in URL mapping as a general solution to many problems.
Another big reason to use something like this is to have RESTful URLs.
With Apache, you are able to setup URL Rewriting for your php pages with mod_rewrite, check this resources:
- mod_rewrite: A Beginner's Guide to URL Rewriting
- Module mod_rewrite
- URL Rewriting Guide
Other than using mod_rewrite, as already reported you can do a little of magic with a simple trick.
Put into the .htaccess a directive like this one
<FilesMatch "^servlet$">
ForceType application/x-httpd-php
</FilesMatch>
substitute ^servlet$ with a regular expression of your choice (it will be the name of your dispatcher)
The file servlet should be similar to this
<?php
$data = explode('/',$HTTP_SERVER_VARS['PATH_INFO']); // $data[0] always empty
$fileToInclude = $data[1].'.php';
if (file_exists($data[1]) {
$params=array_slice($data,2); // you can do here something more sophisticated
// for example sanitize parameters or assemble
// an hash
include ($fileToInclude); //Think to this file as a servlet
} else {
// issue a 404 error, maybe one of the 500 series
}
?>
The url can have the form: http://yoursite/servlet/reports/sales/2009
you can also reach the form http://yoursite/reports/sales/2009
plaiyng a little with the .htacces and the dispatcher.
This method has the advantage that mod_rewrite is not required as FilesMatch (1.3+) and ForceType (2.0+) are in the apache core
See for reference
http://httpd.apache.org/docs/2.2/mod/core.html#forcetype
http://httpd.apache.org/docs/2.2/mod/core.html#filesmatch
http://www.devarticles.com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/
A trick I use is the $ _ SERVER['PATH_INFO'] (I think you need Apache). This gets you all of the information after the PHP file extension.
http://www.example.com/page.php/rest/url
the $ _ SERVER['PATH_INFO'] will contain:
"/rest/url"
The page can then redirect, process some code or whatever based on that string.
This is good if you don't have access to the Apache Configs or .HTACCESS, Also if things change you can make page.php a directory name for static file placement. Oh and PHP doesn't need to have a .php extension. I've used .api or somesuch in the past.
http://www.example.com/worker.api/rest/url
That seems to work well for me.
Can't be done in PHP as such. Note that in Java, it's not the Servlets either that map URLs, but it's the configuration of the servlet container (e.g. Tomcat).
You can achieve similar results for PHP applications by using URL rewriting.
Options for HTTP servers other than Apache:
If you are using Lighttpd, then their mod_rewrite module has a completely different configuration syntax. See http://redmine.lighttpd.net/wiki/1/Docs:ModRewrite for details.
If you are using IIS, then you need ISAPI_Rewrite (http://www.isapirewrite.com/) or the URL Rewrite Module (http://learn.iis.net/page.aspx/460/using-url-rewrite-module/).
As others have mentioned you can use mod_rewrite to do this but also you could use a front controller design pattern which is what many of the web frameworks use. Take a look at Horde routes or Zend_Controller_Router_Rewrite which can be used for MVC,
This sounds like something that mod_rewrite would typically be used for (as a part of Apache, not PHP.) I have not heard of anything in PHP which would accomplish this.