Url Mapping in PHP

URL mapping in PHP?

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

Mapping URL into custom files

if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.

The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.

the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>

How to Implement URL Routing in PHP

If you use Apache you can do the URL routing via mod_rewrite.

Small example:

RewriteEngine On
RewriteRule ^(dir1)/?(path2)? main.php?dir=$1&path=$2

That'll have any request like

http://yoursite.com/dir1/path1 

served by

http://yoursite.com/main.php?dir=dir1&path=path2

More examples here.

The other alternative have every request redirect to a single php file

RewriteEngine On
RewriteRule (.*) main.php?request=$1

and then to do it in code, where you can use a similar approach, by having a set of regular expressions that are matched by some code and then redirected via header() or just internally.

URL mapping with Azure and PHP

Well, Azure Web Apps use IIS to host your application. The best equivalent of Apache's .htaccess system is actually using web.config file to handle URL rewrite in IIS.

For example, you can just put the following web.config file into your web app's root folder to hide .php extension. For more info, please refer to https://www.saotn.org/iis-url-rewrite-hide-php-extension.

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="hide .php extension" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

REST URLs mapping and physical implementation in PHP

In general

  1. You should redirect all of your requests to one file with htaccess+mod_rewite.
  2. Then you should parse URI with PHP.
  3. A Controller class to decide what action to execute basing on parsed parameters.

For example:

www.domain.com/api/book/1234

You parse it to:

action: book
id: 1234

So you run showBook() action with parameter 1234 - showBook(1234), which will do all the rest of work.

But...

I recommend to use some simple framework for REST applications. For example Slim Framework.



Related Topics



Leave a reply



Submit