Closed Thread Icon

Topic awaiting preservation: An Apache URL Rewriting Challenge Pages that link to <a href="http://ozoneasylum.com/backlink?for=13113" title="Pages that link to Topic awaiting preservation: An Apache URL Rewriting Challenge" rel="nofollow" >Topic awaiting preservation: An Apache URL Rewriting Challenge\

 
Author Thread
Brian Sexton
Nervous Wreck (II) Inmate

From: Sunnyvale, California (USA)
Insane since: Nov 2001

posted posted 03-28-2004 05:25

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. http://www.ozoneasylum.com/Forum12/HTML/001349.html caught my attention (I spent some time sifting through old posts with the help of the forum search engine), but it turned out to be about a related but distinctly different task.

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.

abb
Bipolar (III) Inmate

From: Victoria, BC
Insane since: Mar 2002

posted posted 03-28-2004 09:05

Brian:

You were almost right on with your rewrites. After playing around with them for a bit, I got it to work on my server (Apache 1.3.27)

code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteRule ^([^/]*(\..*)?)$ /path/to/file/$1



This will rewrite a request of http://hostname.tld/whatever/goes/here to /path/to/file/whatever/goes/here
NOTE: If you put this in a .htaccess file (which you are), the /path/to/file/$1 is relative to the directory of the .htaccess file.
For example:
The .htaccess is in /usr/home/mywebsite/
A request comes in for http://hostname.tld/a/file
The webserver will server /usr/home/mywebsite/path/to/file/a/file

Please tell me if this helps!


Edit: Dang auto URL recognition

________
Bradford

[This message has been edited by abb (edited 03-28-2004).]

[This message has been edited by abb (edited 03-28-2004).]

Brian Sexton
Nervous Wreck (II) Inmate

From: Sunnyvale, California (USA)
Insane since: Nov 2001

posted posted 03-30-2004 07:44

Thank you for the suggestion. Is the [^/]* bit because the leading slash may or may not be supplied by the server? If that is the case, shouldn't the quantifier be a question mark instead of an asterisk to denote zero or one rather than zero or any greater quantity?

Through experimentation, I have managed to come very close to what I want to do, but one last thing eludes me. The following fails to return default files except for the root, for which it returns the true root default file rather than the new root default file:

code:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld$ [NC]
RewriteCond /home/user/site/temporary/test%{REQUEST_URI} -f
RewriteRule ^(.*)$ /home/user/site/temporary/test%{REQUEST_URI}


This works: http://www.domain.tld/file.ext
It returns /home/user/site/temporary/test/file.ext

This works: http://www.domain.tld/subtest/file.ext
It returns /home/user/site/temporary/test/subtest/file.ext

This FAILS: http://www.domain.tld/
It returns /home/user/site/<default file> because the condition evaluates as false.

This FAILS: http://www.domain.tld/directory/
It returns a 404 error message because the condition evaluates as false.

Things seem to work the same with or without an L flag trailing the RewriteRule line.

I am getting close! I know that -f flag is for files only, so I may need to address the directory issue with a second structure, although it seems preferable to do it all in one. Hmm...

[This message has been edited by Brian Sexton (edited 03-30-2004).]

Brian Sexton
Nervous Wreck (II) Inmate

From: Sunnyvale, California (USA)
Insane since: Nov 2001

posted posted 03-30-2004 08:23

I believe I have found the solution:

code:
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:

code:
<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; you can find my current contact information at http://www.briansexton.com/.

[This message has been edited by Brian Sexton (edited 03-30-2004).]

abb
Bipolar (III) Inmate

From: Victoria, BC
Insane since: Mar 2002

posted posted 04-01-2004 22:14

I'm glad you've figured it out... I kinda missed the meat of the problem

________
Bradford

« BackwardsOnwards »

Show Forum Drop Down Menu