Skip to main content

Redirect ancient browsers

Dev
Table of Contents

什麽年代了,還有人用 MSIE 5 和 Firefox 1-3。所以今天做了一些小動作。提醒這些用戶要更新到最新的版本了。

每個游覽器打開一個鏈接時都帶有 header,其中就有 用戶代理( User-Agent)。我們就根據這些用戶代理的參數作出分析來网址重定向。

Nginx
#

衹需要 Nginx http map module 模塊。安裝 Nginx 時,默認安裝了,我們衹需要修改一下配置。

例子:nginx.conf 文件

http {

...

map $http_user_agent $ancient_ua {
	default               0;
	"~MSIE\ [0-5]\."      1;
	"~Firefox\ [0-3]\."   1;
	"~Firefox\/[0-3]\."   1;
}

server {
	...

	if ( $ancient_ua ) {
		return 302 http://upgrade.example.com;
	}

}

這樣 MSIE 0-5 或者 Firefox 0-3 的游覽器(當然,要有 User-Agent 的 HTTP header 才能過濾)就自動重定向到 upgrade.example.com,然後 upgrade.example.com 怎樣設計大家就自由發揮。