PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Dec 11, 2017 9:58 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sat Mar 27, 2004 10:09 pm 
Offline
Forum Contributor

Joined: Thu Apr 18, 2002 5:33 pm
Posts: 116
Today is the first day of my short spring break, so it is time to turn my attention back to unfinished books and unfinished software development projects. Here is something I thought of long ago:

I have been wondering if I can dynamically rewrite the URL for any file or directory that is requested via a particular host name when multiple host names point to the same Web space. If so, I could enable customizable domain parking and small-scale virtual hosting for Web sites that do not need their own individual logs and such (notices about domain changes, quick reference pages, etc.). Here is an example of the sort of page for which something like this is useful:

http://www.monthlycalendars.com/

That is a one-page Web site that I have up to tell visitors to the old domain that the name of the site and its corresponding domain have both changed.

Unfortunately, I have thus far been unable to redirect an entire domain to a subdirectory without redirecting each individual file like so:

RewriteCond %{HTTP_HOST} ^(www\.)?somedomain\.tld$ [NC]
RewriteRule ^file.ext$ /directory/directory/file.ext

I have tried various permutations of the following in .htaccess files (my Web sites are hosted on shared servers, so I do not have access to the server configuration files), but without success:

RewriteCond %{HTTP_HOST} ^(www\.)?somedomain\.tld$ [NC]
RewriteRule ^/(.*) /directory/directory/$1 [L]

Any ideas? http://httpd.apache.org/docs/mod/mod_rewrite.html and http://httpd.apache.org/docs/misc/rewriteguide.html provide some useful information and the "Moved DocumentRoot" example on the latter page looked promising, but a working solution--if indeed one exists--thus far eludes me.

For those still reading this uncharacteristically long forum post, my Web sites are currently running under Apache 1.3.7 on Debian GNU/Linux (kernel version 2.4.24).

Thank you in advance for any good information.


Last edited by Brian on Tue Mar 30, 2004 3:29 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: My Solution
PostPosted: Tue Mar 30, 2004 3:26 am 
Offline
Forum Contributor

Joined: Thu Apr 18, 2002 5:33 pm
Posts: 116
I believe I have found the solution:
Syntax: [ Download ] [ Hide ]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld$ їNC]
RewriteCond /home/user/<path to new Web root>%{REQUEST_URI} -f їOR]
RewriteCond /home/user/<path to new Web root>%{REQUEST_URI} -d
RewriteRule ^(.*)$ /home/user/<path to new Web root>%{REQUEST_URI} їL]

Note that while it might seem logical to test for directories before files, Web sites--even small ones--usually contain more files than directories, so testing for files before directories is a minor optimization. Also, note that the path to the new Web root should NOT be followed by a slash as the REQUEST_URI environment variable includes the slash that immediately follows the host name.

Of course, the above code includes just the most relevant parts. Here they are in context:
Syntax: [ Download ] [ Hide ]
<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld$ їNC]
RewriteCond /home/user/<path to new Web root>%{REQUEST_URI} -f їOR]
RewriteCond /home/user/<path to new Web root>%{REQUEST_URI} -d
RewriteRule ^(.*)$ /home/user/<path to new Web root>%{REQUEST_URI} їL]

</IfModule>

Thanks to all who pondered my challenge. I hope someone else finds my solution useful. If you find any errors or vulnerabilities in my solution (I am concerned about security, but I am NOT a security expert) or make any improvements, please let me know. In addition to posting your findings to this thread for public knowledge, I would appreciate being contacted directly regarding such issues as I do not often read old threads in discussion forums except when searching for particular information and thread reply notification does not always seem to work for these forums (or at least, I do not always seem to receive notification messages); you can find my current contact information at http://www.briansexton.com/.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group