By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,445 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,445 IT Pros & Developers. It's quick & easy.

Virtual Directories?

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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/direct...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
Jul 17 '05 #2

P: n/a
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
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #3

P: n/a
Nice solution, anyway: There's a slightly better way ;)

Shawn Wilson <sh***@glassgiant.com> wrote:
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>
<Files scripts>
ForceType application/x-httpd-php
AcceptPathInfo On
</Files>

Shawn Wilson <sh***@glassgiant.com> wrote: 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);


Use
$trailingbits = $_SERVER['PATH_INFO'];
instead.

I've used a similar script, the sources are:
<http://test.dangerouscat.net/download/.htaccess>
<http://test.dangerouscat.net/download/download>
(The script itself is located in /download, therefore you will see that it
works ;))
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #4

P: n/a
Simon Stienen wrote:

Nice solution, anyway: There's a slightly better way ;)
<snip>
<Files scripts>
ForceType application/x-httpd-php
AcceptPathInfo On
</Files>
<snip>
Use
$trailingbits = $_SERVER['PATH_INFO'];
instead.


<snip>

That is a bit cleaner. Wish I'd known...

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #5

P: n/a
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
Jul 17 '05 #6

P: n/a
Berislav Lopac wrote:
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


c) ISAPI_rewrite on IIS

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.