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.
SOLVED: An Apache URL Rewriting Challenge
Moderator: General Moderators
SOLVED: An Apache URL Rewriting Challenge
Last edited by Brian on Tue Mar 30, 2004 2:29 am, edited 1 time in total.
My Solution
I believe I have found the solution:
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:
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/.
Code: Select all
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]Of course, the above code includes just the most relevant parts. Here they are in context:
Code: Select all
<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>