Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Problem

subdomains

         

japhar

12:08 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Hello,
I've got some problems with Rewrite.
My DocumentRoot in vhosts.conf is /home/user/domain.com/
My directory structure is:

/home/user/domain.com/html/ = http://domain.com
RewriteRule ^sub - [L]
RewriteCond %{REQUEST_URI} !^/html
RewriteRule ^(.*)$ html/$1

/home/user/domain.com/sub/subdomain1/ = http://subdomain1.domain.com
/home/user/domain.com/sub/subdomain2/ = http://subdomain2.domain.com

I've setup this using this rewrite code:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com [NC]
RewriteRule!^/*sub/ sub/%2%{REQUEST_URI}
and this works great.

Now i've got question. Is there a possibility to rewrite any requests to nonexisting (subdomains/subdirectories) ie.
http://nonexisting.domain.com to /home/user/domain.com/html?
(if i type http://nonexisting.domain.com it will show content of /home/user/domain.com/html without changing URL in browser)

Thanks in advance.
Japhar

[edited by: jdMorgan at 6:22 pm (utc) on Nov. 21, 2005]
[edit reason] De-linked example domains. [/edit]

jdMorgan

3:37 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



japhar,

Welcome to WebmasterWorld!

This seems like a tough problem. Try something like this:


# Skip if main domain requested
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
# Else extract subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com [NC]
# Save subdomain-subdirectory and requested URI-path in user variables, chain to next rule
RewriteRule ^/(.*)$ - [E=subdir:%2,E=URIreq:$1,C]
# If subdomain/subdirectory exists as a directory
RewriteCond %{DOCUMENT_ROOT}/html/sub/%{ENV:subdir} -d
# Rebuild URI using saved variables and rewrite to it
RewriteRule !^/sub/ /sub/%{ENV:subdir}/%{ENV:URIreq} [L]
# Else rewrite to main domain index page
RewriteRule .* / [L]

This is not tested, and will probably need adjustment. The purpose is to demonstrate a method to save working variables and check for the existance of the requested subdomain-subdirectory.

I removed the check for blank HTTP_HOST, since if it is blank the second RewriteCond will fail anyway, so it's not needed.

Jim

japhar

4:46 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Hello Jim,

Thank You for fast respond.

I modify a little your's code:

# Skip if main domain requested
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
# Else extract subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com [NC]
# Save subdomain-subdirectory and requested URI-path in user variables, chain to next rule
RewriteRule ^/(.*)$ - [E=subdir:%2,E=URIreq:$1,C]
# If subdomain/subdirectory exists as a directory
RewriteCond %{DOCUMENT_ROOT}/sub/%{ENV:subdir} -d
# Rebuild URI using saved variables and rewrite to it
RewriteRule!^/sub/ /sub/%{ENV:subdir}/%{ENV:URIreq} [L]
# Else rewrite to main domain index page
RewriteRule .* / [L]

In example:
1) I request in browser http://www.asde.domain.com/qwer.html
2) Then my %{ENV:subdir}=asde and %{ENV:URIreq}=qwer.html
3) Check %{DOCUMENT_ROOT}/sub/asde is a existing directory
( %{DOCUMENT_ROOT} = DocumentRoot from vhosts.conf )?
4) If YES rewrite internal to /sub/asde/qwer.html
5) If NOT rewrite to / which is equal http://www.domain.com

Am I right or totaly wrong?

Japhar

[edited by: jdMorgan at 6:22 pm (utc) on Nov. 21, 2005]
[edit reason] De-linked example domain. [/edit]

jdMorgan

5:31 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In your previous post, you said the site was located in /html below DocumentRoot, so check that. If the file-exists check fails, it will do so 'silently' and the only indication that the filepath is wrong will be that the rule will not be invoked. So this part is hard to debug, and it's better to check carefully that the path is correct. Yes, %{DOCUMENT_ROOT} is taken from the DocumentRoot configuration in httpd.conf.

Otherwise, test it when you site traffic is lowest and see if it works. You could checge the code to test for a specific subdomain, like 'test' and get that working first, and then test it for all subdoamins after that.

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com [NC] matches either www.<subdomain>.domain.com or <subdomain>.domain.com, but not www.domain.com, so you might want to use it as I posted if you get type-in URLs.

Jim

japhar

6:01 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Yes, my site is located in /html below document root:
/home/user/domain.com/html
but subdomains are located in:
/home/user/domain.com/sub not in
/home/user/domain.com/html/sub
That's why i modified yours code :)

Otherwise i owe you a good polish beer, or two, or even more =)

It's working. Thanks Jim

Japhar

jdMorgan

6:11 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> i owe you a good polish beer, or two, or even more =)

Cheers! (Okrzyki salutowac!)
Jim