2

I am trying to achieve virtual subdomain as my hosting provider has set a limitation on the number of subdomains I can have! I already have a wildcard DNS entry so all subdomains are accepted and send to the root of my domain. Here's what I have put in .htaccess to handle the redirection:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www|admin).* [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.domain\.-com
RewriteCond /home/username/public_html/subdomain/%1 -d
RewriteRule ^(.*) /home/username/public_html/subdomain/%1/$1

The problem is in the last line which does the actual redirection, the server shows a 500 (Internal server error), but the apache error log does not expand the error. In its place if I put something like:

RewriteRule ^(.*) index.php?path=/home/username/public_html/subdomain/%1/$1

It works fine... And the path is the correct file path.

2
  • Final solution: RewriteEngine on # first prevent recursion RewriteCond %{ENV:REDIRECT_STATUS} ^$ # second handle subdomains RewriteCond %{HTTP_HOST} !^(www|admin)\.* [NC] RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.domain\.com RewriteCond /home/username/public_html/subdomain/%1 -d RewriteRule (.*) /subdomain/%1/$1 [L]
    – GKK
    Dec 4, 2011 at 11:59
  • hi GKK, can you update your Question with your comment, or add it as an answer.
    – Toby Allen
    Dec 4, 2011 at 20:02

2 Answers 2

1

Although its late, but here's the full answer to my question. It's a pretty handy script if you ever run out of subdomains.

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^[www|admin] [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.domain\.com [NC]
RewriteRule (.*) /subdomain/%1/$1 [L]

Note the actual URL for: http://virtual.domain.com/index.php will be http://www.domain.com/subdomain/virtual/index.php

0

I would advice to:

  • first test if it's a subdomain (like you do) and if so make your tests on /subdomain/(realsubdomain)
  • then if it's (www or admin) then refuse everything that tries to access to /subdomain/xxx

Note that I've removed (/home/username/public_html)

So this should be close to:

RewriteEngine on
# first handle subdomains
RewriteCond %{HTTP_HOST} !^(www|admin)\.* [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.domain\.com
RewriteCond /home/username/public_html/subdomain/%1 -d
RewriteRule (.*) /subdomain/%1/$1
# then in main domains:
RewriteCond %{HTTP_HOST} ^(www|admin)\.* [NC]
# ...refuse access to subdomain:
RewriteRule ^/subdomain(.*) - [R=404,L]

NB: this is a 404 redirect, so you could make your own 404 at the very beginning of the rewriterules, something like:

ErrorDocument 404 /404.php
RewriteEngine on
RewriteRule /404.php - [QSA,L]
... blabla...

And in your 404.php handle the URL to display some nice message :)

Two hints:


Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

5
  • @Oliver Even this: RewriteRule (.*) /subdomain/%1/$1 throws a 500 HTTP error! I cannot understand this issue logically because if I can write RewriteRule ^(.*) index.php then RewriteRule (.*) subdomain/%1/$1 (no leading /) should work !
    – GKK
    Dec 3, 2011 at 14:14
  • I've added my two usual "hints", could you please add rewrite log in your question to see what's going on? Thanks Dec 3, 2011 at 16:49
  • @Oliver: Thanks a lot for your help..! Since this setup is on a shared hosting server, I cannot have RewriteLog enabled there, so I have set up a similar environment on my system. The problem is that it gets stuck in an infinite loop and keeps adding /subdomain/.. to every request and hence the 500 error.
    – GKK
    Dec 4, 2011 at 11:39
  • @Oliver: I have figured it out! Adding RewriteCond %{ENV:REDIRECT_STATUS} ^$ as the first condition prevents the rewrite loop which solves the problem.
    – GKK
    Dec 4, 2011 at 11:48
  • @GKK Okay I didn't know about the REDIRECT_STATUS environment variable ;) Glad to help anyway! Dec 4, 2011 at 12:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .