Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL mapping in PHP?

Tags:

rest

php

apache

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.

like image 567
cletus Avatar asked Dec 28 '08 01:12

cletus


People also ask

What is a URL mapping?

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 ( /* ).

How to do URL routing in PHP?

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.

What is PHP in a URL?

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.

How do I get the URL of a PHP file?

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.


2 Answers

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
like image 168
Christian C. Salvadó Avatar answered Oct 09 '22 03:10

Christian C. Salvadó


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/

like image 35
Eineki Avatar answered Oct 09 '22 05:10

Eineki