301 Redirect

Sergey A. Osokin osa at freebsd.org.ru
Thu Nov 24 20:51:07 UTC 2022


Hi there,

On Wed, Nov 23, 2022 at 08:38:27PM -0500, davidcapital wrote:
> Hello, I would like to know the code to redirect a subdomain to mobile,
> could anyone help me?
> 
> ex:RewriteCond %{HTTP_USER_AGENT} "ipod|iphone|ipad|android|palm" [NC]
> 
> RewriteRule (.*) http://m.yoursite.com.br/[R=301,L]

It's possible to configure nginx with the map directive [1] with the
$http_user_agent embedded variable [2], something like this (not tested):

map "$http_user_agent" $mobile {
    default      0;
    "~*ipod"     1;
    # define other user agents here
}

server {
    ...
    if ($mobile) {
        return 301 http://m.yoursite.com.br;
    }
}

References
1. http://nginx.org/en/docs/http/ngx_http_map_module.html#map
2. https://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_

Hope that helps.

-- 
Sergey A. Osokin



More information about the nginx mailing list