Using folder structure, URL, instead of parameters, setup a virtual host, apache httpd-vhosts.conf, and then grab the url via PHP

This post is about creating a small application in a folder, that behaves like Zend, or you could also say that it behaves like a service, in that it uses (converts) the folder structure of the url for the application’s use, rather than passing it url parameters. Another common example of such a system is employed by WordPress when “clean urls” is turned on.

 

Instead of this: http://alexyz.com?someparameter=something

We want this: http://alexyz.com/something

 

PHP then acquires that last value (call it a “command” or an “action” or whatever you’d like) and proceeds…

 

First, setup an apache virtual host, so that everything after the folder your app will live in is redirected back to that folder’s index along with any following parameters.  You do this via httpd-vhosts.conf

 

example contents of httpd-vhosts.conf (there are several virtual host projects in this example, for independant Zend applications, the code relevant to this lesson is red):

 

# Setup Listening Port
NameVirtualHost *:80

# Ensure “localhost” is preserved
<VirtualHost *:80>
ServerName localhost
DocumentRoot “C:\xampp\htdocs”

<Location /html/bonus>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
RewriteRule ^.*$ /html/bonus/index.php [NC,L]
</Location>

</VirtualHost>

# Setup “components” Virtual Host
<VirtualHost *:80>
ServerName components
DocumentRoot “C:\xampp\htdocs\components\public”

<Directory “C:\xampp\htdocs\components\public”>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
RewriteEngine off

<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Location>

</VirtualHost>

# Setup “myzendresource” Virtual Host
<VirtualHost *:80>
ServerName myzendresource
DocumentRoot “C:\xampp\htdocs\myzendresource\public”
<Directory “C:\xampp\htdocs\myzendresource\public”>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

# Setup “datamodels” Virtual Host
<VirtualHost *:80>
ServerName datamodels
DocumentRoot “C:\xampp\htdocs\datamodels\public”
<Directory “C:\xampp\htdocs\datamodels\public”>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

 

So, any Url after /bonus, in this example, like: http://www.alexyz.com/bonus/something

will be pointed back to /bonus/index.php by apache for us.

 

Next, we simply grab that last part of the url in our index.php (in this example it’s the value “something”).  There are many ways we could do this, the example here is a very basic implementation of one way we might grab that value and proceed…

 

< ? php

$parameters = explode(“/”, $_SERVER[“REQUEST_URI”]);
foreach($parameters as $param){
if($param != NULL) { // This won’t fire if there is a terminating slash, we don’t want to set the parameter to empty!

$bonus_parameter = $param;

}
}

 

// now we can proceed with index.php just as though we’d done this, but we haven’t…:

 

$param = $_GET[‘parameterName’];

 

 

Tags: , ,

Leave a Reply

You must be logged in to post a comment.