Friday, March 20, 2015

URL Routing in PHP


How to Implement URL Routing in PHP

Among the major aspects observed by search engines to determine the relevance of a particular page to a specific search term is that whether the URL link itself embraces a certain word or not. The instant ability to define your URLs in a more human readable, SEO friendly format and to remap old URLs to novel functionality is the key reason for organizations to adopt for URL routing.

What is URL Routing?
URL Routing is a mechanism used to map URLs to the code that gets executed only when a certain request is received at the server.
Why you need URL Routing?
URL Routing makes your URLs considerably more meaningful. Suppose, you have a page that searches for books based on the author’s name. It may appear like this:
index.php?action=search&type=book&author=fitzgerald
Such type of URLs are difficult to understand and remember for the user. Therefore, to make these URLs more user friendly, you use ‘URL Routing’ and transform these URLs into simpler format.
/search/book/ftizgerald
Another great advantage of URL routing is found in Search Engine Optimization and there is no denying to the fact that strong SEO can enhance the positioning of your website and eventually profitability.
How to implement URL Routing in PHP?
The following guide will help you in implementing URL routing in PHP and Apache.
  1. First of all, make sure you have installed and enabled rewrite module in Apache’s configuration
  2. Now, we have to create a .htaccess file in root directory of our website
    1
    2
    3
    4
    
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [QSA,L]
    * Check, if the mod_rewrite has been enabled or not. If, yes then the following configuration will be used.
    * ‘RewriteEngine On’ enables the rewrite engine. Next, we will handle all the requests that do not correspond to the file names. After that, we will pass the requests to index.php file. Now, index.php will manage all the requests.
  3. For every page request, index.php will be executed. In order to distinguish between the different requests, you will have to use $_SERVER['REQUEST_URI']
  4. Next, simply explode $_SERVER['REQUEST_URI'] with ‘/’ and you will get the substrings that will help you to compare and execute the code corresponding to them.
The Real Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 /*
 The following function will strip the script name from URL i.e.  http://www.something.com/search/book/fitzgerald will become /search/book/fitzgerald
 */
 function getCurrentUri()
 {
  $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
  $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
  $uri = '/' . trim($uri, '/');
  return $uri;
 }
 
 $base_url = getCurrentUri();
 $routes = array();
 $routes = explode('/', $base_url);
 foreach($routes as $route)
 {
  if(trim($route) != '')
   array_push($routes, $route);
 }
 
 /*
 Now, $routes will contain all the routes. $routes[0] will correspond to first route. For e.g. in above example $routes[0] is search, $routes[1] is book and $routes[2] is fitzgerald
 */
 
 if($routes[0] == “search”)
 {
  if($routes[1] == “book”)
  {
   searchBooksBy($routes[2]);
  }
 }
Conclusion
This is how you will be able to implement minimal URL routing mechanism. There are a plenty of PHP frameworks that provide enhanced URL Routing features and you can try them as well.