Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Need some help to redirect to a mobile subdomain. Currently both m.domain.com and domain.com will go to var/www/html/mobile. Obviously I need domain.com to direct to var/www/html and m.domain.com to var/www/html/mobile

I have disabled ServerName and my aliases are correct..

Currently my virtualhosts are:

<VirtualHost *:80>
ServerName DOMAIN.com
DocumentRoot "/var/www/html"
</VirtualHost>

#VirtualHost to redirect m.DOMAIN to mobile directory
<VirtualHost m.DOMAIN.com:80>
ServerName m.DOMAIN.com
DocumentRoot "/var/www/html/mobile"
</VirtualHost>

Could someone please point me in the right direction?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

What you're looking for is name-based virtual hosts. Apache has a good document covering it here, but essentially, you just need to set a NameVirtualHost directive and have it match the host/port definititions in your VirtualHost sections.

Something like this:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName domain.com
    DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *:80>
    ServerName m.domain.com
    DocumentRoot /var/www/html/mobile
</VirtualHost>
share|improve this answer
    
MANY thanks bro, worked perfectly! Always those little things causing the problems ;) –  aciid Nov 10 '11 at 21:19

To add to Shane Madden's post, you need something like the following to actually have your primary site redirect to the mobile site if it's being browsed by a mobile browser.

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    DocumentRoot /var/www/html
    RewriteEngine on
    RewriteCond  %{HTTP_USER_AGENT} "iphone|ipod|android|blackberry|opera mini" [NC,OR]
    RewriteCond  %{HTTP_USER_AGENT} "pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine" [NC,OR]
    RewriteCond  %{HTTP_USER_AGENT} "iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile" [NC,OR]
    RewriteCond  %{HTTP_USER_AGENT} "mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo" [NC]
    RewriteCond  %{HTTP_USER_AGENT} "!ipad|xoom" [NC]
    RewriteRule ^(.*) http://m.domain.com$1 [R,L]
</VirtualHost>

<VirtualHost *:80>
    ServerName m.domain.com
    DocumentRoot /var/www/html/mobile
</VirtualHost>
share|improve this answer
1  
Thanks for that, already have javascript redirecting but I guess virtualhosts would be better, since not all mobiles support javascript. –  aciid Nov 10 '11 at 21:26
    
wish I could favorite this answer! That rewrite trick is pretty cool :) –  warren Nov 11 '11 at 0:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.