homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Wildcard subdomains and a custom subdomain

 10:40 am on Dec 16, 2011 (gmt 0)

Well I am trying to setup wildcard subdomains and it is working with the following rules.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com(:80)?$
RewriteRule ^$ /section.php?slug=%1 [QSA,L]

This is working great. All dubdomains are retrieved with section.php. However, now I want to add a custom subdomain for mobile. I want to use m.domain.com and the contentof that domain should come from the map in the root /m. I thought it was easy, but I can't get it working.

I tried a lot but nothing works, can some one give me a clue?

For example m.domain.com/logo.gif should come from www.domain.com/m/logo.gif.

I thought I could simply copy the above code and adjust it a little bit, but it keeps appending the directory.

I tried:

RewriteCond %{HTTP_HOST} ^m\.domain\.com(:80)?$
RewriteRule ^(.*)$ /m/$1 [QSA,L]


RewriteCond %{HTTP_HOST} ^m\.domain\.com(:80)?$
RewriteRule ^(.*)$ m/$1 [QSA,L]


RewriteCond %{HTTP_HOST} ^m\.domain\.com(:80)?$
RewriteRule ^(.*)$ m$1 [QSA,L]

but nothing seems to work! Can someone please help me?



 11:31 am on Dec 16, 2011 (gmt 0)

I'm not a real great expert but, have your considered exclude the m from the rule above?

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|m)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com(:80)?$
RewriteRule ^$ /section.php?slug=%1 [QSA,L]

and below try with the rules you've already tried.


 4:18 pm on Dec 17, 2011 (gmt 0)

Yes I tried that, but doesn't work.

If i open m.domain.com it goes to m.domain.com/m


 10:23 pm on Dec 17, 2011 (gmt 0)

Time for a chorus of

. . . but only what I tell it

It is going to m.domain.com because you haven't told it to go somewhere else. If your Redirect begins in a slash-- this applies to both mod_alias and mod_rewrite-- it will reappend the existing domain name, whatever it was. If you want it to go to a different domain name, you have to lay out the whole thing, beginning with the protocol. In fact you should get in the habit of doing this with all redirects, unless you specifically want them to stay where they were.

But you don't want a redirect, do you? You want a rewrite, so the user sees


while getting content from


That puts you into proxy territory.

Go to rewrite_rule [httpd.apache.org] and then scroll down to the table headed "all possible substitution combinations". You want the very last item in the table, right?

^/somepath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo
via internal proxy

"Internal rewrite" isn't an option. Once you've changed domain names, it's either "external redirect" or "internal proxy". So part of what you want is

RewriteCond %{THE_REQUEST} blahblah
RewriteRule ^(.*)$ http://www.example.com/m/$1 [P,QSA,L]

I'm not going to fill in the blahblah because, one, it's good for your soul to figure it out yourself, and two, I would probably get it wrong ;) But you need to look at what the user originally asked for, so you don't end up going around in circles. The [L] flag is not strictly necessary: it's implied, as with [F] and [G] (but not [R]). But it won't do any harm.


 12:51 pm on Dec 18, 2011 (gmt 0)

Well I have a wildcard subdomain, so the following is also ok:

The user sees:

while getting content from


 10:51 am on Dec 19, 2011 (gmt 0)

In your first post you said
For example m.domain.com/logo.gif should come from www.domain.com/m/logo.gif.

Have you changed your mind or did one of us misunderstand something? :(


 6:00 pm on Dec 19, 2011 (gmt 0)

Well i realized that i can also come from the sam subdomin so i don't have to use redirects because the domain is not the same.... Someboy know where to find more about th / and rewriterule.


 12:15 am on Dec 20, 2011 (gmt 0)

Is that the consistent pattern?

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.com
RewriteRule (([^/]+/)*[^/]*) /%1/$1 [L]

You need one more Condition to make sure the address (the real-life one, not the originally requested one) is not already /m/etcetera. I will leave that part for someone who had more than three hours of sleep last night ;)

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