homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Searching for a kind of subdomain script (mod rewrite?)

 9:35 pm on Sep 14, 2003 (gmt 0)

Hi there,
i need some help for my little subdomain script.

Ok, here is an example:

domain.com & www.domain.com <- if this is used, then do nothing

lala.domain.com, 123.domain.com ... and ALL other subdomains <- if this is used, then load a php file ->
htdocs/index.php?subdomain=lala or 123 ...

Its hard to explain that in english, becouse iam german.
But maybe u understand me and can help me :)




 2:05 am on Sep 15, 2003 (gmt 0)

Welcome to WebmasterWorld [webmasterworld.com], CyBeR!

Unfortunately, according to the charter [webmasterworld.com], which the TOS [webmasterworld.com] says you're supposed to read before posting in this forum, you need to show that you've done your share of the work.

Here's a thread to get you started:
[webmasterworld.com ]


 3:39 am on Sep 15, 2003 (gmt 0)

OK, sorry for that. I searched and browsed the forum ...

I found a thread with this here:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^user\.example\.com [NC]
RewriteRule ^(.*)$ /blog.php?username=user [R=301]

I rewrite it to this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^user\.example\.com [NC]
RewriteRule ^(.*)$ /subdomain.php?username=user [R=301]

It works, but how can i do that www.example.com & example.com dont use the RewriteRule?
I hope i may reqeuest this here, else iam sorry for it :)


 5:31 am on Sep 15, 2003 (gmt 0)

The code you just posted doesn't affect www.example.com, and doesn't affect example.com, so you should be OK. It only affects requests to user.example.com. Or did I misinterpret your question?

However, I would change this line from:

RewriteRule ^(.*)$ /subdomain.php?username=user [R=301]


RewriteRule .* /subdomain.php?username=user [R=301,L]

I simplified the search pattern because the ^ and $ were redundant (the results of the match weren't used later on). I also put in the L flag because the rewriting process ends on that line (although some might argue that I did that just to make jdMorgan happy ;-)).


 6:07 am on Sep 15, 2003 (gmt 0)

For a more general case, which is what you wanted in your first message, the code would go a little something like this:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule .* /subdomain.php?username=%1 [R=301,L]

Walkthrough: If the host isn't www.example.com or example.com (first case in your first message), go on to the second condition. If the host is a subdomain not mentioned in the previous line (second case), proceed to the RewriteRule. Permanently redirect the user to subdomain.php with username set to the matched pattern from the most recent RewriteCond.

I haven't tried running the code I just gave, so there may be something I'm missing. If there is something wrong, your server's logs can help you see what's going on.


 9:21 am on Sep 15, 2003 (gmt 0)

OK, now i use this:
RewriteEngine on
RewriteCond %{HTTP_HOST}!^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule .* /subdomain.php?username=%1 [L]

If i use this here:

RewriteRule .* /subdomain.php?username=%1 [R=301,L]

Then it doesn't work. It load and load an load, but nothing happens.

Big Big Thx TO You :)


 11:11 am on Sep 15, 2003 (gmt 0)

Ah yes...

If you want to use the R flag there, you'd have to use a URI (an address that starts with http://).

Big big you're welcome to you.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved