Forum Moderators: phranque

Message Too Old, No Replies

Rewriting HTTP_HOST to directories

         

tarmon

10:33 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



Good afternoon,

After reading through the mod_rewrite and rewrite guide docs and tinkering with things a bit, I have to admit I'm still in the dark. Here is what I'm attempting to do:

I'm setting up service to offer domain names to users on a free web hosting service. Currently, when a user signs up with the free service, they receive a subdirectory on our site, in the form of /var/www/html/ourhost.com/user. As doing virtual hosts for all the potential users is impractical, and the mass virtual hosting can't translate into this directory structure, I'd like to use a RewriteMap to translate domains to these users, e.g.

test.com user

And build the final path off that

Can someone provide me with a bit of help as to how to accomplish this?

Thank you,

Mike

jdMorgan

11:02 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mike,

Welcome to WebmasterWorld!

You could use RewriteMap, but consider that it might be simpler (practically zero-maintenance) to give each user a subdomain, and then use a subdomain-to-subdirectory rewrite that will handle arbitrary subdomains.

Then all you have to do is check the user applying for a subdomain: Check the username to require that it not be offensive, not contain anything but upper and lowercase characters and hyphens, make sure it's not "www" or "test" or "devel" or "production" or any other subdomains you might want to reserve for testing, and any other checks you might want to do.

If this test passes, then create a subdirectory of %{DocumentRoot}/users/ that corresponds to the subdomain name. Then a simple rewriterule that prepends "?users/" and the subdomain part of %{HTTP_HOST} to the requested local URL-path can be used to map any subdomain to its corresponding subdirectory. You might need a few exclusions for images, scripts, templates, and CSS files that are used globally by all users, but with a little up-front organization, it shouldn't be difficult.

If you search WebmasterWorld for "subdomain subdirectory rewriterule", you'll find several old threads on the subject.

For more general information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

tarmon

11:13 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



JdMorgan,

Thanks for your reply!

However, this is not what I'm after. We already do provide the user a subdomain off our site, in the form of username.ourhost.com. We've received a lot of requests from our users (who pay for no advertising, FTP access, etc) to be able to use their own domain names transparently on our service.

We could do the halfway job of using a hidden frame, or a redirect to the user's existing subdomain on our side, but those aren't desireable and have their own headaches associated with them.

I'm an experienced programmer and have already written the code to do the map creation into our site, but the final piece, the Rewrite ruleset that makes it work, is currently the dealbreaker.

Thanks again,

Mike

jdMorgan

11:25 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, well if you have specific problems or questions about RewriteMap, post 'em up, and we can discuss.

RewriteMaps aren't really too difficult -- It's just the clunky syntax that throws people. They're easy enough to use with text-based lookups or database-access scripts, though.

Jim

tarmon

11:35 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



That's just it. I'm coming up empty on how to do this at all with the RewriteRules.

I have set up an IP based virtual host to do this on, so that the domains pointed to that IP address get rewritten.

Currently, I'm on

RewriteMap domains txt:/var/www/domainforward/domains.txt

and honestly don't know how to proceed with the rewriting beyond that. Any chance you could help me out as to what the chain of events should look like to pick up the hostname, look it up in the map, and rewrite the path to the correct directory?

Either way, I appreciate your time spent replying.

Thanks,

Mike

jdMorgan

11:54 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I hope you'll take this code as an example and work through the documentation line-by-line and check it -- no warranty... :)

# Associate text lookup filepath with the mod_rewrite map name, and declare the map type
RewriteMap domains txt:/var/www/domainforward/domains.txt
# Exclude the main site's domain name (you may not need this depending on vhosts setup)
RewriteCond %{HTTP_HOST} !^(www\.)maindomain\.com
# Exclude common page template directory (just an example if you need it)
RewriteCond %{REQUEST_URI} !^/templates/
# Create back-reference to domain name, less "www" and .com TLD
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.com
# Pass domain name (in var %2 from above line) as lookup key to map and rewrite
RewriteRule ^/(.*) /${domains:%2¦NULL}$1 [L]

If a mapped subdirectory is not found in domains.txt, then the default behaviour is to leave the URL-path as it was.

As this example is coded, subdirectory entries in domains.txt should have a trailing slash, but no leading slash.

Have fun!

Jim

tarmon

12:03 am on Feb 11, 2006 (gmt 0)

10+ Year Member



Thanks for your help Jim. Now I can enjoy my weekend. I'll give it a run on Monday and see how it turns out.

Have a good weekend!

Mike