Forum Moderators: phranque

Message Too Old, No Replies

htaccess Domain name to folder

Pointing a domain name to a sub folder

         

craig_lock

10:40 am on Mar 10, 2005 (gmt 0)

10+ Year Member



I have a client who has like 80 odd sites, they are not that important to know the stats etc.. so instead of setting them up on the lovely control panel my ISP has (which would be tedious)

I have pointed them all to the same hosting account and they all have their own folder within i this..

I have tried with htaccess to push which ever domain name you put view the folder so domain2.com would goto domain.com/domain2 and have the URL keep as domain2.com

I couldn't get that to work, so i used PHP to do that, but now coz of that all the files etc.. are not beng found, a long winded approach would be to do this..

# DEFAULT SHARES
RewriteRule ^scripts/(.*)$ /shared/scripts/$1 [L]
RewriteRule ^images/general2/invis.gif /shared/images/general2/invis.gif [L]
RewriteRule ^images/towns/townslarge/(.*)$ /shared/images/towns/townslarge/$1 [L]
RewriteRule ^images/events/eventssmall/(.*)$ /shared/images/events/eventssmall/$1 [L]
RewriteRule ^images/prices/(.*)$ /shared/images/prices/$1 [L]
RewriteRule ^images/buttons/moreinfo/(.*)$ /shared/images/buttons/moreinfo/$1 [L]
RewriteRule ^images/buttons/buyit/(.*)$ /shared/images/buttons/buyit/$1 [L]
RewriteRule ^form/(.*)$ /shared/form/$1 [L]

# DOMAINS
RewriteRule ^css/(.*)$ /domain2/css/$1 [L]

RewriteRule ^images/general/(.*)$ /domain2/images/general/$1 [L]

RewriteRule ^images/buttons/(.*)$ /domain2/images/buttons/$1 [L]

RewriteRule ^contact.htm /domain2/contact.htm [L]

RewriteRule ^booking.php /domain2/booking.php [L]

RewriteRule ^enquiry.php /domain2/enquiry.php [L]

RewriteRule ^thanks.htm /domain2/thanks.htm [L]

which of course with 80 odd domains would be tedious..

So i was wondering if there was a way i gould manipulate the actuall domain name (minus the www. and tld) to give me the folder to point the files to?

Any help would be gratefully appreciated..

jdMorgan

5:45 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



craig_lock,

Welcome to WebmasterWorld!

There is a method to rewrite any subdomain request to a unique subdirectory [webmasterworld.com] using mod_rewrite in .htaccess. Unfortunately, this method only works if your server supports POSIX 1003.2 regular expressions, which includes a new "atomic back-references" feature. This is supported on FreeBSD servers, and possibly a few others.

The main problem you have to deal with in .htaccess is preventing an "infinite loop" of rewriting. Because you're using mod_rewrite in a per-directory (.htaccess) context, the output of any rewriting done at the driectory level must be passed back through httpd.conf and again through any .htaccess files in the filepath of the newly-rewritten URL -- in order to check for access restrictions or to invoke further rewrites which apply to the new path. So the trick is to avoid having your rewrites invoked multiple times, which will cause a loop and usually lead to a 500-Server Error.

This can be done in several ways. The code cited above does it by checking to see if the current URL already contains the subdomain name. But it relies on an operating system feature not available in most servers.

Another way to do it is to insert a common exclusion element in the URL -- something that can be detected to avoid recursive rewriting, without having to explicitly test for each subdomain name. For example, rewrite:


www.blue.example.com/file --> /subd/blue/file
red.example.com/file ---------> /subd/red/file

Here we use the path element "subd" as a constant that we can check, in order to avoid a rewrite loop.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subd/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule (.*) /subd/%2/$1 [L]

So, the rewrite is only done if the current URL does NOT start with "/subd/". Otherwise, the subdomain (following an optional "www.") is copied into the new subdirectory path, along with the originally-requested filename.

Here's another tip that may save you some work: You can include alternates in back-references. For example,

RewriteRule ^contact\.htm /domain2/contact.htm [L]
RewriteRule ^booking\.php /domain2/booking.php [L]
RewriteRule ^enquiry\.php /domain2/enquiry.php [L]
RewriteRule ^thanks\.htm /domain2/thanks.htm [L]

Can be restated as:

RewriteRule ^(contact\.htm¦booking\.php¦enquiry\.php¦thanks\.htm)$ /domain2/$1 [L]

This technique can also be applied to your other rules -- this is just a simple example.

Change the broken pipe "¦" characters to a solid pipe characters before trying to use this. (Posting on this baord modifies that character).

Jim

craig_lock

9:52 am on Mar 17, 2005 (gmt 0)

10+ Year Member



Thanks for the help i managed to do it for each domain, but it picks up the first site that i put up and not the site that i am acytually looking at.

Ie: it looks at the rewriteRules for domain 1 where i am looking at domain 3.

So i want to do a lil bit of code to say if the domain is this do this. i tried:

RewriteCond %{HTTP_HOST} ^http://(.+\.)?example\.co.uk [NC]
RewriteRule ^css/(.*)$ /example/css/$1 [L]
RewriteRule ^images/general/(.*)$ /example/images/general/$1 [L]
RewriteRule ^images/buttons/(.*)$ /example/images/buttons/$1 [L]
RewriteRule ^(contact\.htm¦booking\.php¦enquiry\.php¦thanks\.htm)$ /example/$1 [L]

but doesn't wanna work.. Any help would be appreciated.

[edited by: jdMorgan at 4:54 pm (utc) on Mar. 17, 2005]
[edit reason] Removed specifics per TOS. [/edit]