Forum Moderators: phranque

Message Too Old, No Replies

Subdomain pointing to sub-directories

         

ebustop

6:02 am on Nov 14, 2003 (gmt 0)

10+ Year Member



Hi,

Pretty new to mod_rewrite stuff, hence not understanding the syntax fully as from other posts. So decided to ask you experts directly.

With /home/www, as the home directory for my main domain (itself a virtualhost), I'm trying to set sub-domains to point to sub-directories.
eg.

mail.mydomain.com points to /home/www/mail
test1.mydomain.com points to /home/www/testing/test1
test2.mydomain.com points to /home/www/testing/test2

I'm trying to find out what to add into htaccess file to make this work. Where should the htaccess file reside? on /home/www? Or on each sub directory with a different rewrite condition?

Thanks.

Have the following relevant info in httpd.conf :

# mydomain.com
<VirtualHost NNN.NNN.NNN.NNN>
DocumentRoot /home/www
ServerName www.mydomain.com

UseCanonicalName off

RewriteEngine On
RewriteMap lowercase int:tolower

RewriteCond %{HTTP_HOST}!^(.*)mydomain.com
RewriteCond %{HTTP_HOST}!^NNN.NNN.NNN.NNN.*

RewriteRule ^/(.*)$ /home/www/${lowercase:%{HTTP_HOST}}/$1
RewriteRule ^/home/www/www\.(.*)$ /home/www/$1

RewriteCond %{REQUEST_URI} ^/cgi-bin/.*
RewriteCond %{HTTP_HOST}!^(.*)mydomain.com
RewriteCond %{HTTP_HOST}!^NNN.NNN.NNN.NNN.*

RewriteRule ^/(.*)$ /home/www/${lowercase:%{HTTP_HOST}}/$1
[T=application/x-httpd-cgi]
RewriteRule ^/usr/www/www\.(.*)$ /home/www/$1
[T=application/x-httpd-cgi]

</VirtualHost>

jdMorgan

1:56 am on Nov 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ebustop,

As stated in our charter, we prefer not to write your code for you, but to help you write your own. Here's an example off one of my sites that rewrites requests for http://test.example.com/ to the subdirectory /test on the server. This code is installed in .htaccess in the web root directory of the site.


RewriteEngine on
# Redirect test subdomain to subdirectory /test
RewriteCond %{HTTP_HOST} ^test\.example\.com
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule (.*) /test/$1 [L]

Refs:
Introduction to mod_rewrite [webmasterworld.com]
Apache mod_rewrite documentation [httpd.apache.org]

Jim

ebustop

10:05 am on Nov 15, 2003 (gmt 0)

10+ Year Member



Actually I have tried several combinations, and now tried the one above as well. But it does not seem to be working. Is there some option in httpd.conf above that is preventing this to work properly?

Just a note that the httpd.conf, is not modifiable by me. My host is a defined virtual host in the httpd.conf using an IP.

jdMorgan

5:02 am on Nov 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try adding "Options +FollowSymLinks" at the very beginning.

Better yet, try a simple test. Place the following in your .htaccess file in the web root directory on your server (where your "home page" goes):


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /index.html [L]

Assuming that "index.html" is a simple html file that exists, then if you request "silly.html" from your site using your browser, your server should return the contents of index.html.

If this does not work, then your server is probably not configured to allow mod_rewrite.

Jim

ebustop

5:49 am on Nov 16, 2003 (gmt 0)

10+ Year Member



Yup silly.html is working. Even this is :

RewriteEngine on
Options +FollowSymLinks
RewriteRule ^silly\.html$ /test5/index.html [L]

I'm still thinking its some condition/rule in httpd.conf that is blocking this. Just not sure which one might be causing some conflict.

My directory structure is as follows :

[/usr/home/myhome] % ls -l www
lrwxr-x--x 1 myhome 84 36 Nov 15 21:17 www -> /usr/home/myhome/mydomain.com/htdocs

[/usr/home/myhome] % ls -l mydomain.com

drwxr-xr-x 11 myhome 84 512 Nov 15 10:54 htdocs

jdMorgan

3:46 pm on Nov 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ebustop,

If the code worked with Options +FollowSymLinks placed *after* RewriteEngine on, then you do not need Options +FollowSymLinks at all. If you had needed it, the RewriteEngine on directive would have caused an error, because FollowSymLinks is needed to 'enable' the rewrite engine. FollowSymLinks is enabled by default in httpd.conf, otherwise it would not have worked.

You haven't really said what happens when you try to do the rewrite. I would recommend looking at the raw error log if you get any errors. You may need to use the RewriteBase directive if your server seems to be looking in the wrong path for the substituted directories and files.

Sometime it's even useful to force an error - like a 404 - to see what shows up in the error log. Redirect to a file you *know* does not exist, and then check the error log.

Your httpd.conf code apparently allows arbitrary subdomain names (unless I'm missing something), so I don't see why httpd.conf would be interfering with the redirect.

Jim

closed

4:31 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



What code are you using, ebustop?

And what do you mean when you say your code doesn't work?

ebustop

4:25 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



ok guys this mod rewrite seems to be working its wonder. Based on the first reply in this folder :

RewriteEngine on
# Redirect test subdomain to subdirectory /test
RewriteCond %{HTTP_HOST} ^test\.example\.com
RewriteCond %{REQUEST_URI}!^/test/
RewriteRule (.*) /test/$1 [L]

lets say now I have other subdomains, will I have to repeat these same 3 lines for each subdomain?

Thanks once again.

jdMorgan

4:42 pm on Nov 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, not necessarily...

You can *try* this - I didn't test it. %1 is used as a back-reference to pick up and re-use the subdomain name from the parenthesized subexpression of the second RewriteCond:


RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www'
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule (.*) /%1/$1 [L]

Jim

ebustop

1:43 am on Dec 18, 2003 (gmt 0)

10+ Year Member



Following the above suggestion, I am trying as below :

RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www'
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST}!^mail\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteCond %{REQUEST_URI}!^/%1/
RewriteRule (.*) /mydata/%1/$1 [L]

What I am trying to do is to redirect all canonical names like 'abc.example.com' to 'www.example.com/mydata/abc', excluding 'www.*' and 'mail.*'.

But its erroring in the log that MaxReDirects has been exceeded. RewriteOptions MaxReDirects is set to 20 currently. Seems a loop is occurring?

jdMorgan

4:40 am on Dec 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like it.

Add a RewriteCond to stop it:


RewriteCond %{REQUEST_URI} !^/mydata/

I have tried the same trick you've used there with !^/%1/ recently, and it does not work for some reason, so you have to do the subdomain/subdirectory exclusion 'manually' by listing the subdirectory names explicitly. I even tried putting the subdomain/subdirectory name into an environment variable and using that variable to try to exclude it, but that did not work either. mod_rewrite apparently does not resolve any variable on the right side of a RewriteCond - the pattern there must be a plain-text-based regular expression. :(

Jim