For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > September 2004 > Virtual Directories?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Virtual Directories?
Purple Haze

2004-09-27, 8:56 pm

I have seen websites that use a sort of "virtual directory" system...

Lets say when you click on something it goes to
http://www.example.com/scripts/php/

Neither the scripts directory nor the php directory physically exisit on
the server, does anyone know how this is done?
Thomas Mlynarczyk

2004-09-28, 8:56 am

Also sprach Purple Haze:

> I have seen websites that use a sort of "virtual directory" system...
>
> Lets say when you click on something it goes to
> http://www.example.com/scripts/php/
>
> Neither the scripts directory nor the php directory physically exisit
> on the server, does anyone know how this is done?


The apache web server can automatically transform a path into another using
the Alias directive in its configuration files:

Alias /scripts/php c:/just/another/directory

So, when you type http://www.example.com/scripts/php/, apache goes to
c:/just/another/directory instead.

However, there is a much more flexible solution with Apache, it's called
mod_rewrite. This is an apache module that allows you to transform
(re-write) URLs whichever way you wish, depending on the URL and the
environment. Example:

RewriteEngine On
RewriteRule ^scripts/php/(.*)$ /my/own/directory/myscript.php?page=$1

So, when you type http://www.example.com/scripts/php/welcome.html, the above
rule (to be placed either in the Apache's config file or an .htaccess file)
will transform this into
http://www.example.com/my/own/direc...e=welcome.html. The
syntax of mod_rewrite uses regular expressions and is a bit complicated (I'm
not even sure if my above example will actually work, but it illustrates the
principle.) But it can be a very powerful tool.

Greetings,
Thomas


Shawn Wilson

2004-09-28, 9:26 pm

Purple Haze wrote:
>
> I have seen websites that use a sort of "virtual directory" system...
>
> Lets say when you click on something it goes to
> http://www.example.com/scripts/php/
>
> Neither the scripts directory nor the php directory physically exisit on
> the server, does anyone know how this is done?


Hey,

http://www.example.com/scripts/php/

1. Create a file called "scripts" in the web root.


2. Make a .htaccess file to force apache to interpret "scripts" as PHP

<Files scripts>
ForceType application/x-httpd-php (going from memory. Google "ForceType")
</Files>


3. Manually get the trailing bits and use them to search a database or whatever.

$possiblevalues = array('php', 'cgi', 'perl', 'asp');
$trailingbits = preg_replace('/^scripts\//', '', $_SERVER['REQUEST_URI']);
if (!in_array($trailingbits, $possiblevalues))
show_my_error('Invalid script type');
else
list_scripts($trailingbits);

Shawn

--
Shawn Wilson
shawn@glassgiant.com
http://www.glassgiant.com
Berislav Lopac

2004-09-29, 10:45 am

On Mon, 27 Sep 2004 22:35:08 GMT, Purple Haze wrote:

> I have seen websites that use a sort of "virtual directory" system...
>
> Lets say when you click on something it goes to
> http://www.example.com/scripts/php/
>
> Neither the scripts directory nor the php directory physically exisit on
> the server, does anyone know how this is done?


a) mod_rewrite on Apache
b) custom 404 error page
Justin Koivisto

2004-09-29, 10:45 am

Berislav Lopac wrote:

> On Mon, 27 Sep 2004 22:35:08 GMT, Purple Haze wrote:
>
>
>
>
> a) mod_rewrite on Apache
> b) custom 404 error page


c) ISAPI_rewrite on IIS

--
Justin Koivisto - spam@koivi.com
http://www.koivi.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com