Forum Moderators: phranque
/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]
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]
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
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]
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
Otherwise i owe you a good polish beer, or two, or even more =)
It's working. Thanks Jim
Japhar