Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite and Third Level Domain

         

decola

10:20 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



Hi all!
My troubles are about Apache Rewrite conditions and Third Level Domain.

My directories structure is below:

/var/www/www.example.com/www
/var/www/www.example.com/user1
/var/www/www.example,com/user2
......................
....................
.....................

I need that if an user asks for AAAA.example.com, Apache returns files in /var/www/www.example.com/AAAA/

This in my /etc/apache2/sites-available/www.example.com:

<VirtualHost *>
ServerName www.example.com
ServerAlias www.example.com *.example.com
DocumentRoot /var/www/www.example.com

<Directory /var/www/www.example.com>
RewriteCond %{HTTP_HOST} ^forum\.example\.com
RewriteCond %{REQUEST_URI} !^/forum/?
RewriteRule ^(.*)$ /forum/$1 [L]
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

The problem is every third level domain registered to DNS server (i.e. forum.example.com, user1.example.com) points to /var/www/www.example.com.
That is, if an user asks for
http://forum.example.com
the server returns:
/var/www/www.example.com/index.html
and not
/var/www/www.example.com/forum/index.html

Can somebody helps me?

Thanks in advance.

[edited by: jdMorgan at 11:19 pm (utc) on Feb. 3, 2006]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

11:14 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first rule should be:

RewriteRule ^/(.*)$ /forum/$1 [L]

Otherwise, you'll rewrite a request for /abc.php to /forum//abc.php

But that does not look like the main problem. You wrote

That is, if an user asks for http://forum.example.com
the server returns: /var/www/www.example.com/index.html

The first thing I noticed is the missing server config:


Port 80
ServerName local.example.com
#
# Your IP address goes on these lines
NameVirtualHost *:80
#
<VirtualHost *:80>
ServerName www.example.com
ServerAlias [b]ex[/b]ample.com *.example.com
DocumentRoot /var/www/www.example.com

See [httpd.apache.org...]

Also, your RewriteRule *does not* add "index.html" to the URL. So there is some other code somewhere that is adding "index.html" and that may be where your problem is.

You can turn on rewrite logging and define a rewrite log file to help you find this problem, since you have access to the server config. See RewriteLog and RewriteLogLevel in the mod_rewrite documentation. Warning: Don't forget to turn logging off when you are done testing; It consumes a lot of server resources when logging, especially if you set the loglevel to a high number.

Jim

extras

5:59 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



I feel that it's better to create another virtualhost for the forum subdomain.
Otherwise, you may experience problems with some scripts.
(Links showing unwanted /froum/ directory, for example.)

In other words, I'd avoid using RewriteRule for doing that,
and try the method that gives exact DOCUMENT_ROOT, as much as possible.

decola

1:43 pm on Feb 6, 2006 (gmt 0)

10+ Year Member



Good News! If an user requesthttp://forum.example.it Apache responses with /var/www/www.example.it/forum.
Below is /etc/apache2/sites-available/www.example.it file:

<VirtualHost *>
ServerName www.example.it
ServerAlias www.example.it *.example.it
DocumentRoot /var/www/www.example.it

RewriteCond %{HTTP_HOST} ^forum\.example\.it
RewriteCond %{REQUEST_URI}!^/forum/?
RewriteRule ^/(.*)$ /forum/$1 [L]

<Directory /var/www/www.example.it>
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

I would like to modify this configuration to allow any third level domain (i.e. feedback.example.it, news.example.it,...) and not just forum.example.it domain.

I've tried this:

RewriteCond %{HTTP_HOST} ^(.*)\.example\.it
RewriteCond %{REQUEST_URI}!^/$1/?
RewriteRule ^/(.*)$ /$1/$2 [L]

but it doesn't work.

Can anybody help me?
Thanks in advance.

jdMorgan

4:34 pm on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteCond cannot compare two variables, so that's why your code doesn't work. A variable can appear in the left expression of a RewriteCond, but the right expression must be a fixed text value or regular expression (or a syatem function flag such as -f).

The simplest solution involves a slight change to your subdomain-directory names:


RewriteCond %{REQUEST_URI} !^/sdd_
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.it
RewriteRule ^/(.*)$ /sdd_%2/$1 [L]

This requires that your subdomain subdirectories be named starting with "sdd_". This makes it easy to check the request to be sure it hasn't already been rewritten in order to prevent rewrite looping. This prefix is arbitrary; You can use any prefix you like, as long as it is unique and does not appear in any of your other existing subdirectory names.

Jim

decola

4:45 pm on Feb 6, 2006 (gmt 0)

10+ Year Member



You are a very kind guru!
Thanks a lot!