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.

I am trying to adding a subdomain for my mobile site. But when I add below code into my httpd.conf, i could open m.domain.com in my browser, but failed all www.domain.com with 500 Internal Server Error return.

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>

Here is my full httpd.conf, where am i wrong?

ServerTokens OS
ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 60
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule prefork.c>
StartServers       3
MinSpareServers    3
MaxSpareServers    10
ServerLimit        256
MaxClients        256
MaxRequestsPerChild  1024
</IfModule>
<IfModule worker.c>
StartServers       1
MaxClients        100
MinSpareThreads    1
MaxSpareThreads    4
ThreadsPerChild     25
MaxRequestsPerChild  0
</IfModule>
Listen 80
Include conf.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
ServerName www.domain.com:80
UseCanonicalName Off
DocumentRoot "/var/www/html"

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>

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
<IfModule mod_userdir.c>
    UserDir disabled
</IfModule>
DirectoryIndex index.html index.html.var
AccessFileName .htaccess

BTW: how to set a different ip for my subdomain?

for an exapmle, my main domain ip is 92.1.2.3, and i want to setting ip 92.1.2.4 to my subdomain, many thanks.

EDIT 1

Now, i only add these lines at the end of the https.conf.

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    DocumentRoot /var/www/html
    RewriteRule ^(.*) http://m.domain.com$1 [R,L]
<directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from all
</directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName m.domain.com
    DocumentRoot /var/www/html/mobile
<directory "/var/www/html/mobile">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from all
</directory>
</VirtualHost>

then move RewriteRule into wordpress .htaccess,

    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteBase /
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
    </IfModule>

    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]

it works. but a new problem apears. when i access http://m.domain.com, the httpd/error_log will print Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.. How to fix this problem? thanks again.

share|improve this question
    
If you want different IPS, just change the *:80 to 92.1.2.3:80 and 92.1.2.4:80 on the domains you want. –  NickW Apr 30 '13 at 9:10
    
@NickW, thanks, i will try, but at first, when i setting subdomain in httpd.conf, i can not access in my main site. –  cj333 Apr 30 '13 at 9:13
1  
Why do you have the Server Name outside the virtual hosts, if you have an alias of the same name inside one the hosts? –  NickW Apr 30 '13 at 9:20
2  
What is appearing in the apache errorlog? –  Zabuzzman Apr 30 '13 at 9:22
    
@NickW, so how to setting correctly? –  cj333 Apr 30 '13 at 9:25
show 7 more comments

1 Answer

up vote 0 down vote accepted
+50

Your current mod_rewrite rules have a redirect loop. Both in the apache config, and .htaccess

You can resolve this by either creating seperate .htaccess files for domain.com and m.domain.com or by excluding m.domain.com from the rewrite rules. The following exclusion would be required for each rule you have checking if mobile, whether its in .htaccess or the conf file.

To disable m.domain.com from looping, add an exclusion (line 2)

RewriteEngine on
RewriteCond  %{HTTP_HOST} !^m\.domain\.com$ [NC]
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]

Current logic:

Is the client using a mobile browser? Yes, Redirect to m.domain.com. Connect, is browser mobile? Yes, Redirect, etc, etc, etc.

If you created separate .htaccess files for each domain, you could reverse the code for m.domain.com, redirecting them to domain.com if they are NOT using a mobile browser.

Binding to specific IP

<VirtualHost 92.1.2.3:80>
    ServerName domain.com
...
<VirtualHost 92.1.2.4:80>
    ServerName m.domain.com
...

Note:

The ServerName directive does not require a port number.

ServerName www.domain.com:80

should be:

ServerName www.domain.com
share|improve this answer
add comment

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.