[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Apache2 & mod_rewrite

Simo Kauppi ha scritto:

On Mon, Sep 12, 2005 at 10:06:06PM +0200, Marco wrote:
Simo Kauppi ha scritto:

It seems to me that the conditions for the first domain say that
any hostname which is not www.doamin1.com should go to www.domain1.com.
I.e. Apache is doing exacly as asked (domain2.com != www.domain1.com) :)

I guess you want requests to http://domain2.com to go to the second
virtual host and www added at the front?  IIRC Apache looks for the
virtual host files in /etc/Apache2/sites-enabled/ directory in the order
their appear. That's why I use the 001-site1, 002-site2 etc to make them
appear alphabetically in the right order.

But back to your question. You need a condition to tell domain1 that it
only takes requests for domain1, i.e.,

VirtualHost for site www.domain1.com
RewriteCond %{HTTP_HOST}   ^.*domain1\.com [NC]

RewriteCond %{HTTP_HOST}   !^www\.domain1\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.domain1.com/$1 [L,R]
or something like that, and same for other hosts as well. It's late so
double-check the syntax :)

And don't forget http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Hope this helps...


Hi Simo,

I have test with your settings but I have still problems. This is the configuration of 000-domain1.com

This is the configuration for 001-domain2.com


ServerName www.domain2.com

DocumentRoot /var/www/domain2/

RewriteCond %{HTTP_HOST}   ^.*domain2\.com [NC]
RewriteCond %{HTTP_HOST}   !^www\.domain2\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.domain2.com/$1 [L,R]

These are the results:

http://domain1.com --> http://www.domain1.com OK (and I see domain1 site)
http://domain2.com --> http://domain2.com NO!!!! (and I see domain1 site)!!!

I would want http://domain2.com --> http://www.domain2.com (and I see domain2 site)

Where is the mistake??


Hi Marco,

Just like Angelo suggested, the configuration needs the ServerAlias
directives. If the requested domain does not match any ServerName, the
first one is served. And then the RewriteCond directive I suggested
earlier should not be needed.



ServerName www.domain1.com
 ServerAlias domain1.com *.domain1.com
DocumentRoot /var/www/domain1/

RewriteCond %{HTTP_HOST}   ^.*domain1\.com [NC]

RewriteCond %{HTTP_HOST}   !^www\.domain1\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.domain1.com/$1 [L,R]

And for the second host ServerAlias domain2.com *.domain2.com

Of course you can change the *. to www if you want those servers only
respond to domainx.com and www.domainx.com.

I have removed the Rewrite rule on my Virtual Host configuration file, I have added the
rule ServerAlias domain1.com *.domain1.com, and I have restart apache2.

Test: http://www.domain1.com --> OK
http://domain1.com --> PAGE NOT FOUND ???

I have also an other question, using the server alias option when a client visit http://domain1.com
its URL comes rewritten in http://www.domain1.com????



Reply to: