301 Redirect

D
  • 24 Nov '22
383 / 5.000
Resultados de tradução
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]

  This one I used in .htaccess in openlitespeed, now I use nginx and the
rules are different. I would like to know how to do this redirection in
nginx.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,295879,295879#msg-295879
S
  • 24 Nov '22
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