Wildcard subdomain rewrite...but rewrite any other calls to www domain

What I need is if someone types in a dynamic subdomain (wildcard) like:

foo.mydomain.com

for it to go to mydomain.com/availability.php?username=foo

and this I have working with this code:


RewriteCond %{HTTP_HOST} ^([^.]*)\\.venueave\\.com$
RewriteCond %{HTTP_HOST} !^www\\.venueave\\.com
RewriteRule (.*) availability.php?username=%1 [QSA]

Now my problem is that there are some ajax calls being made and I need these to be rerouted to the www.mydomain.com version in order for them to work.

So how can I say if there is anything following the “foo.mydomain.com” to run it from the root www.mydomain.com directory?

For example:

foo.mydomain.com/js/ajax.php

should really run

www.mydomain.com/js/ajax.php

Best,

Chris

With some more searching I found my solution:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.mydomain\.com$ [NC]
RewriteRule ^$ availability.php?username=%2 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ $1 [L]

If you know of a better solution please let me know otherwise I’m good to go!

Chris

Chris,

If that’s the case, then you REALLY need to learn how to SELECTIVELY capture “everything” because (.*) is a troublemaker!

GOOD to see that you’ve managed to get dynamic virtual hosting setup! That’s an accomplishment.


RewriteCond %{HTTP_HOST} ^([^.]*)\\.venueave\\.com$
RewriteCond %{HTTP_HOST} !^www\\.venueave\\.com
RewriteRule (.*) availability.php?username=%1 [QSA]
[indent][I]If you're not using the $1, use .? to speed-up processing.[/I][/indent]

Reroute ajax calls?

To do this, you’ll need to add two sections to your programming (scripts and/or mod_rewrite):

  1. WHY are you using foo.domain.com/js/ajax.php to link to your AJAX in the first place? Let sanity prevail and use the absolute link to the location you want to fetch the content.

Okay, if that’s not possible, you can fall back on using mod_rewrite to redirect foo.example.com/js/ajax.php calls to js/ajax.php by …

  1. Exclude js/ajax.php from the existing redirection by adding the exclusion
RewriteCond %{REQUEST_URI} !^js/ajax\\.php$

Of course, loosen that up if you’re also using other scripts for AJAX.

Regards,

DK

You do realize that a website on foo.example.com is not allowed to call a script on [noparse]www.domain.com[/noparse] through AJAX because of the cross domain policy in AJAX right?

There seems to be a workaround for subdomains like you want here. I haven’t tried it though.

Rémon,

Yeah, that’s why …

Regards,

DK