Forum Moderators: phranque
Here it is:
Desired URL
www.user.webspace.mydomain.com
'user' can be anything but the rest is absolute. I need one or more Rewrite rules so that all the below resolve to the above correctly.
>> user.webspace.mydomain.com
Easy peasy this one (note the w* to allow for any number of www typo's).
RewriteEngine On
RewriteCond %{HTTP_HOST}!^w*\.
Rewriterule ^(.*)$ [%{HTTP_HOST}$1...] [R=301,L]
>> user.mydomain.com
This is ok as above would rewrite to the next problem
www.user.mydomain.com
Now I am stuck.. the rule needs to read if the subdomain 'webspace.' is missing and if yes enter it in between 'user' and 'mydomain.com'.
NOTE: this is on a server that although will handle *.mydomain.com apache must only resolve webspace.mydomain.com
I am pretty sure there is something simple but just can't put my finger on it.
Any help would be much appreciated.
Welcome to WebmasterWorld!
Ok, if I understand your question, I'd suggest something like this:
RewriteEngine On
# Fix up odd www typos or completely-missing www prefix
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(ww+\.)?(.+)$
RewriteRule ^/(.*) http://www.%2/$1 [R=301]
#
# Fix up missing 'webspace' if subdomain is www.user
RewriteCond %{HTTP_HOST} !^www\.[^.]+\.webspace\.mydomain\.com
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mydomain\.com
RewriteRule ^/(.*) http://www.%1.webspace.mydomain.com/$1 [R=301,L]
The negative tests are needed to fully qualify the redirects, and also to prevent redirection looping.
Jim
Now I had a little extension on this solution, my server is set as
<VirtualHost *:80>
And I only want 1 VirtualHost entrry; so I need to cater for ANY domain that points within this virtual host to do the same thing, this is what I came up with:
<VirtualHost *:80>
RewriteEngine On
##RewriteCond %{HTTP_HOST} !^w*\.
##Rewriterule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
# Fix up odd www typos or completely-missing www prefix
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(ww+\.)?(.+)$
RewriteRule ^/(.*) http://www.%2/$1 [R=301]
# Fix up missing 'webspace' if subdomain is www.user
RewriteCond %{HTTP_HOST} !^www\.[^.]+\.webspace\.[^.]+\.[^.]+
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.([^.]+)\.([^.]+)
RewriteRule ^/(.*) http://www.%1.webspace.%2\.%3/$1 [R=301,L]
ServerAdmin %2@%4+
VirtualDocumentRoot "/home/customers/vhosts/%2.1%2.2/%2.-2%2.-1/%2@%4+"
ErrorLog logs/error_log
CustomLog logs/access_log common
VirtualScriptAlias "/home/customers/vhosts/%2.1%2.2/%2.-2%2.-1/%2@%4+/cgi-bin/"
RLimitMem 5120000 10485760
RLimitCPU 2 4
RLimitNPROC 3 5
<Directory /home/customers/vhosts>
Options ExecCGI +Includes
</Directory>
# ErrorDocument 404 /home/customers/vhosts/%4+
# ErrorDocument 400 /home/customers/vhosts/%4+
</VirtualHost>
This works perfect EXCEPT it just caters for top level domains, .com .net etc any ideas how I could also allow for .co.uk etc withing the same elements?
[edited by: jdMorgan at 4:07 pm (utc) on Mar. 4, 2005]
[edit reason] Formatting fix, removed quoted post. [/edit]
# Fix up missing 'webspace' if subdomain is www.user
RewriteCond %{HTTP_HOST} !^www\.[^.]+\.webspac[b]e\.[/b]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.[b](.+)[/b]
RewriteRule ^/(.*) http://www.%1.webspace.%2/$1 [R=301,L]
The second RewriteCond is then changed to accept the entire "end" of the hostname, without regard to "dot" separators.
Jim