Page 1 of 1

"virtual path" / path translation on Apache + PhP

Posted: Wed Mar 03, 2010 2:10 am
by easydev
Not sure whether it is more related to Apache or PhP, but is there a way that I can use a common handler to handle all the requests, if the URL is not found? (something similar to the dhandler in perl/mason)

What I am trying to do is:
I want to setup a kind of URL "translator", for example,
Given a request to URL like http://www.myhost.com/a1/a2/a3.php, I actually want http://www.myhost.com/b1.php to handle it (of course, I don't want the client to know it -- I do not want to simply redirect the request to the actual location).
Given another request to URL like http://www.myhost.com/c1/c2/c3.php, I actually want http://www.myhost.com/d4/d5.php to handle it.

Of course, we can assume there are some kind of rules for the url translation.

Any idea of this?

Thanks a lot.

Re: "virtual path" / path translation on Apache + PhP

Posted: Wed Mar 03, 2010 2:39 am
by jraede
Check this out: Apache Mod_Rewrite. You can do a huge number of things with this.

What I do is just route everything, that doesn't point directly to an existing file, to index.php, and then based on the content of the URL I do require_once on initializers and templates to display what the user wants to see.

My .htaccess file looks like this:

Code: Select all

 
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>