0

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.

cj333
  • 109
  • 11
  • 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 09: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 09: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 09:20
  • 2
    What is appearing in the apache errorlog? – Zabuzzman Apr 30 '13 at 09:22
  • @NickW, so how to setting correctly? – cj333 Apr 30 '13 at 09:25
  • @Zabuzzman, `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.` – cj333 Apr 30 '13 at 09:25
  • That error is due to your rewrite probably, I'll let someone better with apache regexes set you straight. – NickW Apr 30 '13 at 09:35
  • Can you try the request with wget and post us the output? – Zabuzzman Apr 30 '13 at 10:54
  • The one rewrite in the provided config wouldn't cause this problem - are there `.htaccess` files in your content? – Shane Madden May 01 '13 at 04:21
  • @Zabuzzman, sorry, i do not have permission to access ssh. so no `wget` to use. – cj333 May 01 '13 at 18:27
  • @Shane Madden, yes, i have a wordpress default `.htaccess` file. – cj333 May 01 '13 at 18:31
  • @cj333 Can you set a `RewriteLog` location and turn up `RewriteLogLevel 9` so we can see which rules are causing the problem? – Shane Madden May 02 '13 at 15:54

1 Answers1

0

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
David Houde
  • 3,200
  • 1
  • 16
  • 19