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