Mod_rewrite & wildcard subdomains. weird problem with letter 'w'

Ok,
I use mod_rewrite to fetch subdomain from url and direct that to php-script.
In my htaccess file I have:

RewriteCond %{HTTP_HOST} [1]+\.mydomain.fi$
RewriteRule ^$ %{HTTP_HOST} [C]
RewriteRule ^([^\.w{3}]+)\.mydomain\.fi$ http://$1.mydomain.fi/show_user.php?subdomain=$1 [R=301,L]

For example:
test.mydomain.fi [rewrites into] test.mydomain.fi/show_user.php?subdomain=test

That is what I want, now the problems arise when the subdomain contains letter ‘w’ in any position.

Fow example:
testw.mydomain.fi [rewrites into] testw.mydomain.fi

I would greatly appreciate any solution into this.


  1. ^\.w{3} ↩︎

I had similiar problems with wildcard subs…

at the end, I redirected all subs unchanged to a folder, where that script is running…

$hostlist = explode(‘.’,$_SERVER[‘HTTP_HOST’]);
if (ereg(“[1]{2,50}$”,$hostlist[0])) {
$url = ‘http://mydomain.fi/profile/‘.$hostlist[0].’/’;
} else {
$url = ‘http://mydomain.fi’;
}
header('Location: '.$url);

Im sure it’s dirty like a mudbath - but it works fine since 2 years…

regards
hensel


  1. a-zA-Z0-9_- ↩︎

Thanks for replying. I managed to write a working set rules:

RewriteCond %{HTTP_HOST} ^(.)\.mydomain.fi$
RewriteRule ^$ %{HTTP_HOST} [C]
RewriteRule ^(.
)\.mydomain\.fi$ http://$1.mydomain.fi/show_user.php?subdomain=$1 [R=301,L]

Not sure if it could be better. Works anyway now.

murz,

To fetch a subdomain (and exclude www.), one would simply use

RewriteEngine on
# Capture subdomain (EXCLUDE www.)
RewriteCond %{HTTP_HOST} ^([a-z0-5]+\\.)example\\.com [NC]
# now, exclude www.
RewriteCond %1 !www\\.$
RewriteRule .? http://%{HTTP_HOST}/show_user.php?subdomain=%1 [R=301,L]

The differences are:

  1. I actually captured the subdomain with the atom formed by (…)

  2. I specified that the capitalization didn’t matter (it doesn’t in the {HTTP_HOST} string)

  3. I used the captured (subdomain) atom to check against www.

  4. THEN I created LOOPY code to send to show_user.php with the subdomain as the value of subdomain

HINT: Stop the loop by checking whether there is a key named subdomain in the %{QUERY_STRING}

hensel,

“mudbath” is appropriate. If you don’t understand mod_rewrite, that’s one way to go. Some simple (logical) thinking is all that’s required to make mod_rewrite perform magic, though, so go back and give it a try. Have a look through the tutorial linked in my signature for sample code.

murz,

First, PLEASE learn to use [noparse]

 ... 

[/noparse] to wrap your code.

RewriteCond %{HTTP_HOST} ^(.*)\\.mydomain.fi$ # This will NOT match {whatever}.MyDomain.Fi without the NoCase flag
RewriteRule ^$ %{HTTP_HOST} [C] # Chain? Why chain? Why a blank {REQUEST_URI}? Are you serious?
RewriteRule ^(.*)\\.mydomain\\.fi$ http://$1.mydomain.fi/show_user.php?subdomain=$1 [R=301,L] 
# The RewriteRule can only examine the {REQUEST_URI} and you have
# it trying to match the {HTTP_HOST}!!! This will NEVER work!

:nono: Surely you jest!

Regards,

DK

dklynn, thank you.

So I modified your code to stop the loop by checking if QUERY_STRING contains “subdomain”.
The code looks like this now:


#Check if subdomain and value are already set
RewriteCond %{QUERY_STRING} !subdomain=([a-z0-5]+)$
# Capture subdomain (EXCLUDE www.)
RewriteCond %{HTTP_HOST} ^([a-z0-5]+)\\.linkkimuistio\\.fi$ [NC]
# now, exclude www.
RewriteCond %1 !www\\.$
RewriteRule .? http://%{HTTP_HOST}/show_user.php?subdomain=%1 [R=301,L]

Is this what you were asking :slight_smile:

btw, Excellent pages you have. I’m trying to learn this…

BestRegards -Murzum