# Redirect Mobile Users to m.domain.com

**URL:** <https://www.ruby-forum.com/t/redirect-mobile-users-to-m-domain-com/221949>\
**Category:** NGINX\
**Created:** [August 6, 2012, 11:23pm UTC](https://www.ruby-forum.com/t/redirect-mobile-users-to-m-domain-com/221949 "2012-08-06T23:23:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 6, 2012, 11:23pm UTC](https://www.ruby-forum.com/t/redirect-mobile-users-to-m-domain-com/221949/1 "2012-08-06T23:23:03Z")

</div>

Hi all,

I have a wordpress website [http://domain.com](http://domain.com). I have created a mobile  
version of it and put in “m” folder of that domain. I want all my mobile  
users to be redirected to [http://m.domain.com](http://m.domain.com) where [http://m.domain.com](http://m.domain.com)  
will be using a mobile version of the website located in “m” folder. By  
using the below rewrite rules, it does redirect to [http://m.domain.com](http://m.domain.com)  
but it does not display any website from the “m” folder. ( Please help:

* * *

server {  
server\_name [domain.com](http://domain.com) [www.domain.com](http://www.domain.com);  
access\_log /home/nginx/domains/domain.com/log/access.log combined  
buffer=32k;  
error\_log /home/nginx/domains/domain.com/log/error.log;  
root /home/nginx/domains/domain.com/public;

try\_files $uri $uri/ /index.php;

location / {  
index index.html index.htm index.php;  
try\_files $uri $uri/ /index.php?$args;  
}

location ^~ /m/index.html {  
rewrite ^/m/(.\*) [http://m.domain.com/$1](http://m.domain.com/$1) permanent;  
}

# Directives to send expires headers and turn off 404 error logging.

```
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;
}

```

include /usr/local/nginx/conf/staticfiles.conf;  
include /usr/local/nginx/conf/php.conf;  
include /usr/local/nginx/conf/drop.conf;  
#include /usr/local/nginx/conf/errorpage.conf;

}

---

<div class="post-metadata">

**Author:** ![yaronycity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/50afbb/32.png) [@yaronycity](https://www.ruby-forum.com/u/yaronycity)\
**Post date:** [August 6, 2012, 11:34pm UTC](https://www.ruby-forum.com/t/redirect-mobile-users-to-m-domain-com/221949/2 "2012-08-06T23:34:44Z")

</div>

On Tue, Aug 7, 2012 at 4:22 AM, yaronycity [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> server {  
> try\_files $uri $uri/ /index.php?$args;  
> }
> 
> ```
> location ^~ /m/index.html {
> rewrite ^/m/(.*) http://m.domain.com/$1 permanent;
> 
> ```
> 
> }

Should be just ^~ /m/ { … }

Also maybe add the server block which handles the m. domain… server  
{ server\_name [m.domain.com](http://m.domain.com); root /some/where/m; … }

---

<div class="post-metadata">

**Author:** ![yaronycity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/50afbb/32.png) [@yaronycity](https://www.ruby-forum.com/u/yaronycity)\
**Post date:** [August 6, 2012, 11:48pm UTC](https://www.ruby-forum.com/t/redirect-mobile-users-to-m-domain-com/221949/3 "2012-08-06T23:48:46Z")

</div>

On Tue, Aug 7, 2012 at 4:45 AM, yaronycity [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have changed it this, and it still does not work:(
> 
> server {  
> server\_name [domain.com](http://domain.com) [www.domain.com](http://www.domain.com);  
> root /home/nginx/domains/domain.com/public;
> 
> server\_name [m.domain.com](http://m.domain.com) [www.m.domain.com](http://www.m.domain.com);  
> root /home/nginx/domains/domain.com/public/m;

Add as separate server { … }, not merged into one.

---

<div class="post-metadata">

**Author:** ![yaronycity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/50afbb/32.png) [@yaronycity](https://www.ruby-forum.com/u/yaronycity)\
**Post date:** [August 6, 2012, 11:46pm UTC](https://www.ruby-forum.com/t/redirect-mobile-users-to-m-domain-com/221949/4 "2012-08-06T23:46:26Z")

</div>

I have changed it this, and it still does not work:(

server {  
server\_name [domain.com](http://domain.com) [www.domain.com](http://www.domain.com);  
root /home/nginx/domains/domain.com/public;

server\_name [m.domain.com](http://m.domain.com) [www.m.domain.com](http://www.m.domain.com);  
root /home/nginx/domains/domain.com/public/m;

try\_files $uri $uri/ /index.php;

location / {  
index index.html index.htm index.php;  
try\_files $uri $uri/ /index.php?$args;  
}

location ^~ /m/ {  
rewrite ^/m/(.\*) [http://m.domain.com/$1](http://m.domain.com/$1) permanent;  
}

# Directives to send expires headers and turn off 404 error logging.

```
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;
}

```

include /usr/local/nginx/conf/staticfiles.conf;  
include /usr/local/nginx/conf/php.conf;  
include /usr/local/nginx/conf/drop.conf;  
#include /usr/local/nginx/conf/errorpage.conf;

}

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,229364,229367#msg-229367](http://forum.nginx.org/read.php?2,229364,229367#msg-229367)
