Wildcard multiple domains and subdomains with www exception.

Using a single web hosting account to host multiple sites

Wildcard multiple domains and subdomains with www exception.

Postby stanlemon » Mon Dec 03, 2007 3:22 pm

Greetings,
I'd like to have apache configured to handle multiple domains, routing them to directories based upon their domain name (ie. domain.com > /www/domain.com , domain.org > /www/domain.org , etc.) However, in doing so I would also like to rewrite any domain whose first part is "www" to the directory without the prefixed "www". I got close earlier today, but then I ran into conflict with the following in one of my domains:
Code: Select all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) PageSite.php/$1 [L]


So, the configuration needs to allow for the above still as well. What I had done, was used VirtualDocumentRoot /www/%0 and then I was trying to write a rule for mod_rewrite, but I pretty much botched that. I've seen plenty of rule examples that supply the domain name for this, but my problem is that I have four or five (and potentially growing) domain names on this server which I want to handle in this fashion, and I'd rather have one rule to cover them all, rather than multiple.

Can anyone help?

Thank you in advance.

Pax,
- Stan
stanlemon
 
Posts: 5
Joined: Mon Dec 03, 2007 3:17 pm

Postby richardk » Tue Dec 04, 2007 3:27 pm

What version of Apache are you using?

Try
Code: Select all
<VirtualHost *:80>
  ServerName _default_

  DocumentRoot /www
  VirtualDocumentRoot /www/%2+

  Options +FollowSymLinks

  RewriteEngine On

  RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
  RewriteRule ^(/.*)$ /%{HTTP_HOST}/PageSite.php$1 [L]
</VirtualHost>

<VirtualHost *:80>
  ServerName www
  ServerAlias www.*

  DocumentRoot /www
  VirtualDocumentRoot /www/%0

  Options +FollowSymLinks

  RewriteEngine On

  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
  RewriteRule ^(/.*)$ /%1/PageSite.php$1 [L]
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby stanlemon » Thu Dec 06, 2007 7:52 am

Richard,
Thank you for the code - I'm traveling right now and can't connect to the server securely, as soon as I am back I will test this out.

I am running Apache 2.2, it's self-compiled on a CentOS5 box.

Pax,
- Stan
stanlemon
 
Posts: 5
Joined: Mon Dec 03, 2007 3:17 pm

Postby stanlemon » Thu Dec 06, 2007 7:55 am

Richard,
I was just looking at the code you wrote and was wondering... does this redirect all requests all the time to PageSite.php? The items I posted for the htaccess appear sometimes - but not always, some of my domains will use this script. My big concern is for subdomain to map to their directories, except for "www" which should map to the level before it without the www prefix.

Hope that helps/clarifies. Thank you for your assistance with this.

Pax,
- Stan
stanlemon
 
Posts: 5
Joined: Mon Dec 03, 2007 3:17 pm

Postby richardk » Fri Dec 07, 2007 1:24 pm

Yes, the mod_rewrite would run for all domains matched by the <VirtualHost>. If you don't want that, remove the mod_rewrite in the <VirtualHost>s and use .htaccess files in their document roots instead.

Where is abc.example.com supposed to go to?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby stanlemon » Sat Dec 08, 2007 6:19 pm

abc.example.com should map to /www/abc.example.com

Just to clarify...

example.com > /www/example.com
www.example.com > /www/example.com
abc.example.com > /www/abc.example.com
foo.com > /www/foo.com
bar.com > /www/bar.com
abc.bar.com > /www/abc.bar.com
stanlemon
 
Posts: 5
Joined: Mon Dec 03, 2007 3:17 pm

Postby richardk » Sun Dec 09, 2007 10:49 am

Try
Code: Select all
<VirtualHost *:80>
  ServerName _default_

  DocumentRoot /www
  VirtualDocumentRoot /www/%0
</VirtualHost>

<VirtualHost *:80>
  ServerName www
  ServerAlias www.*

  DocumentRoot /www
  VirtualDocumentRoot /www/%2+
</VirtualHost>


And then put the following mod_rewrite in .htaccess files for the domains you want it to run on
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /%1/PageSite.php/$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby stanlemon » Sun Dec 09, 2007 2:53 pm

Is there a way to do it without modifying the original htaccess? I'm concerned about modifying it because two of the domains are mirrored, each has a script which checks out the same source from subversion. As-is, the current htaccess file works, and I'm worried about disrupted the mirrors.

Apologies for the difficulty, the virtualhost items look like they work great though.
stanlemon
 
Posts: 5
Joined: Mon Dec 03, 2007 3:17 pm

Postby richardk » Sun Dec 09, 2007 3:20 pm

Probably, try it.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 20 guests

cron