[REOPENED] Dynamic domain name conversion

Using a single web hosting account to host multiple sites

[REOPENED] Dynamic domain name conversion

Postby mwe » Wed Apr 23, 2008 6:37 am

Currently, we setup our virthosts environment to look at what the site name is (i.e. /web/mysite.com), but with these changes also require us to modify the virthosts.conf file to look for the new directories as well.

One of our clients has a number of these 'short' name sites and wants to migrate over to having the 'full' addressed name instead. I came up with the following theory, but I need some additional eyes to see if this will actually happen:

Code: Select all
<VirtualHost *:80>
   ServerAlias *
   UseCanonicalName Off
   LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon

   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^my([^.]+)\.com [NC]
   RewriteRule ^/(.*)$ http://%1.domain.com/$1 [R=301,L]

   CustomLog /var/web/%0/access_log vcommon
   ErrorLog /var/web/%0/error_log

   VirtualDocumentRoot /web/%0/htdocs
   VirtualScriptAlias /web/%0/cgi-bin

   <Directory /web/%0>
      Options Indexes FollowSymLinks Multiviews +Includes
      AllowOverride All
   </Directory>
</VirtualHost>


When someone visits the site with the 'old' name, these steps would happen:
1) The mod_rewrite module kicks in first
2) RewriteCond detects if the incoming URL has the 'my' prefix on the sitename
2) If so, the RewriteCond/Rule kicks in and strips off the 'my' prefix and moves the real site name over the http pointer.
3) The http pointer then triggers (which in this case is back to itself) and also sets the site with a 301 redirect mark.

When someone visits the site with the 'new' name, these steps would happen:
1) The mod_rewrite module kicks in first
2) The RewriteCond detects that the incoming URL is using the new name (w/o the 'my' prefix) and just skips over the RewriteRule.
3) The mod_vhost_alias module kicks in
4) The vhost_alias swaps the incoming URL for the %0 variable, entering it as needed throughout the template.
5) Site is presented as expected

The code, in theory would remove the need for having to setup Apache redirects if the sitename with a 'my' prefix is attached to it (mysite.com) can be converted to the true site name (site.domain.com).

---

So my question is: 'Can mod_rewrite and mod_vhost_alias work together in the same layout'?

-- Michael
Last edited by mwe on Thu Apr 24, 2008 4:33 am, edited 2 times in total.
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby mwe » Wed Apr 23, 2008 11:12 am

Using mod_vhost_alias, is it possible to have a different log file for each virtual host?

I tried this...

CustomLog /var/web/%0/access_log vcommon
ErrorLog /var/web/%0/error_log

But it seems to want a hard, defined directory name for the log file.

I also tried...

CustomLog /var/web/logs/%0.access_log vcommon
ErrorLog /var/web/logs/%0.error_log

But that didn't work either.

Anyone have any ideas?

-- Michael
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby mwe » Wed Apr 23, 2008 11:56 am

Actually found that mod_vhost_alias can only log to one file, so I just made a work around for it instead using the DNS CNAME as the logging directory base.

Here's the output of the working code...

Code: Select all
<VirtualHost *:80>
   ServerAlias *
   UseCanonicalName Off
   LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon

   # Single location for all Apache access & error logs, delimited by URL
   CustomLog /var/web/virtual.domain.com/virtual.access_log vcommon
   ErrorLog /var/web/virtual.domain.com/virtual.error_log

   # Look for 'my' site names (if any), strip off if found and keep rest of domain
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^my([^.]+)\.com [NC]
   # Attach perm redirect code to old site, then redirect back to itself
   RewriteRule ^/(.*)$ http://%1.domain.com/$1 [R=301,L]

   # Using vhost_alias, goto the true destination for new site
   VirtualDocumentRoot /web/%0/htdocs
   VirtualScriptAlias /web/%0/cgi-bin

   <Directory /web/%0>
      Options Indexes FollowSymLinks Multiviews +Includes
      AllowOverride All
   </Directory>
</VirtualHost>


-- Michael
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby mwe » Thu Apr 24, 2008 4:37 am

I found a small loophole that needs to be corrected. It seems the site's real URL name is found as www.mysite.com rather than just mysite.com. Is there a way of using the [OR] flag to also check in the RewriteCond to see if the incoming site has a 'www' in front of it then strip off the 'my'?

I tried this...

Code: Select all
   RewriteCond %{HTTP_HOST} ^my([^.]+)\.com [NC,OR]
   RewriteCond %{HTTP_HOST} www\.^my([^.]+)\.com [NC]


But to no luck.

-- Michael
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby richardk » Thu Apr 24, 2008 1:08 pm

Code: Select all
   RewriteCond %{HTTP_HOST} ^my([^.]+)\.com [NC,OR]
   RewriteCond %{HTTP_HOST} ^www\.my([^.]+)\.com [NC]

or just
Code: Select all
   RewriteCond %{HTTP_HOST} ^(www\.)?my([^.]+)\.com [NC]

(but using %2 instead of %1).

You could also use two <VirtualHost>s, one with
Code: Select all
ServerAlias my* www.my*

to do the redirect in.

Code: Select all
<Directory /web/%0>

Does the %0 work in <Directory> as well? I thought it was limited to only mod_vhost_alias directives.

Code: Select all
Options Indexes FollowSymLinks Multiviews +Includes

The manual says
http://httpd.apache.org/docs/2.2/mod/core.html#options wrote:Warning

Mixing Options with a + or - with those without is not valid syntax, and is likely to cause unexpected results.

so you should probably remove the +.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby mwe » Tue Apr 29, 2008 4:53 am

richardk wrote:
Code: Select all
<Directory /web/%0>

Does the %0 work in <Directory> as well? I thought it was limited to only mod_vhost_alias directives.


After a deployment into our production environment this morning, I discovered that the %0 doesn't work in the Directory, which, in turn, bombs out Drupal as it needs the all parts of the "Options Indexes FollowSymLinks Multiviews +Includes" to work correctly.

Oh well. Something else to try and research a bit more to see if there is a work around.

-- Michael
mwe
 
Posts: 21
Joined: Fri Aug 10, 2007 7:12 am
Location: North Augusta, SC USA

Postby richardk » Tue Apr 29, 2008 9:44 am

You can use
Code: Select all
<Directory /web>

for it to apply to all sub directories of /web, or you could possibly use <DirectoryMatch>.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 2 guests

cron