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.
A URL map is a set of rules for routing incoming HTTP(S) requests to specific backend services or backend buckets. A minimal URL map matches all incoming request paths ( /* ).
First of all, you will need Apache's (I suppose your webserver is Apache) mod_rewrite to be enabled. Then, you need to create a RewriteRule to redirect everything to your index. php page. It will redirect every request to a file that doesn't exist to index.
Definition of PHP URL. Generally, the URL means Uniform Resource Locater. Likewise, URL in PHP Programming Language is also the same. URL is nothing but a website address. It helps in connecting the client and the server when browsed using a specific link.
To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.
With Apache, you are able to setup URL Rewriting for your php pages with mod_rewrite, check this resources:
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With