Forum Moderators: phranque

Message Too Old, No Replies

Tricky subdomain ReWrite help needed

Tricky subdomain ReWrite help needed

         

baggs

3:22 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



Hi all, bit of a newbie in the rewrite area I'm afraid.

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.

jdMorgan

4:17 am on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



baggs,

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]

It is not possible to match all "ww" typos without running into a problem with a URL like "webspace.mydomain.com." So the above code will handle two, four, or more w's, leaving three (the correct "www") alone.

The negative tests are needed to fully qualify the redirects, and also to prevent redirection looping.

Jim

baggs

2:59 pm on Mar 4, 2005 (gmt 0)

10+ Year Member



That's brilliant, thanks 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]

jdMorgan

4:19 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I presume this is primarily a question about regular-expressions, in which case, the change is minor:

# 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]

Note that the pattern in the first (negative) RewriteCond does not need to be so specific. The only item of interest is that the requested hostname does not contain "webspace" as the second or third-level subdomain. So I simplified it.

The second RewriteCond is then changed to accept the entire "end" of the hostname, without regard to "dot" separators.

Jim