Skip to content

Instantly share code, notes, and snippets.

@vladkras
Last active January 17, 2024 09:59
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vladkras/ad103a0f7116e86696b627508b408546 to your computer and use it in GitHub Desktop.
Save vladkras/ad103a0f7116e86696b627508b408546 to your computer and use it in GitHub Desktop.
NGINX redirect to app store and google play based on user agent
location = /gotoapp {
if ($http_user_agent ~* "iphone|ipod|ipad|appletv") {
return 301 https://www.apple.com/lae/ios/app-store/;
}
if ($http_user_agent ~* "android") {
return 301 https://play.google.com/store;
}
if ($http_user_agent ~* "Windows") {
return 301 https://www.microsoft.com/store/apps?rtc=1;
}
if ($http_user_agent ~* "Linux") {
return 301 https://www.linux.org/;
}
if ($http_user_agent ~* "Mac") {
return 301 https://www.apple.com/mac/;
}
return 301 /cant-detect;
}
@salim7
Copy link

salim7 commented Jan 15, 2021

Thanks for sharing. Worked only with single quotes for me. That is 'iphone|ipod|ipad|appletv' instead of "iphone|ipod|ipad|appletv" for example.

@magnusbae
Copy link

You should really use a 302 not a 301 for this. 301 is "permanent" and gets cached in the browser, deeply.

302 is evaluated on each request.

https://stackoverflow.com/questions/1393280/http-redirect-301-permanent-vs-302-temporary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment