htaccess的重定向到特定网页基于浏览器 [英] .htaccess Redirect to specific webpage based on browser

查看:188
本文介绍了htaccess的重定向到特定网页基于浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要根据他们所使用的浏览器的访问者重定向到我的特定页面。

I want to redirect visitors to my specific page based on which browser they are using.

例如:所有游客将首先访问 http://www.example.com/xy/abc.html

For example: all visitors will first visit http://www.example.com/xy/abc.html.

如果访问者使用Firefox的,他应该被重定向到 http://www.example.com/xy/firefox html的

If the visitor is using firefox, he should be redirected to http://www.example.com/xy/firefox.html

如果访问者使用镀铬,他应该被重定向到 http://www.example.com/xy/chrome html的

If the visitor is using chrome, he should be redirected to http://www.example.com/xy/chrome.html

我要处理这个歌剧,浏览器,Firefox,IE,Safari浏览器和默认重定向非识别浏览器。

I want to handle this for opera, chrome, firefox, IE, safari browser and with a default redirect for non identified browser.

与code任何帮助吗?我试图查找和所有我发现了一个重定向code为所有的浏览器,而不是具体的某一种。

Any help with the code? I tried looking up and all I found was a single redirect code for all browser rather than a specific one.

您的帮助是AP preciated。谢谢你。

Your help is appreciated. Thanks.

推荐答案

您可以使用mod_rewrite的的RewriteCond指令测试对用户代理的报告名称。在放置在XY目录中的.htaccess文件,它会是这样的:

You can use mod_rewrite's RewriteCond directive to test against the reported name of the user agent. In a .htaccess file placed in your xy directory, it'd look like this:

RewriteCond %{HTTP_USER_AGENT} Opera
RewriteRule ^abc.html$ http://example.com/xy/opera.html [R=301]

这将永久重定向,在他们的用户代理字符串opera.html有歌剧某处浏览器。你可以找到用户代理是如何确定自己在 useragentstring.com 一个体面的名单。你会发现,很多用户代理字符串实际上以Mozilla开头(由于一些邪恶的历史原因),但只是为了测试,看看是否字符串包含浏览器的名称(即是MSIE)就足够了。

This will permanently redirect browsers that have Opera somewhere in their user agent string to opera.html. You can find a decent list of how user agents identify themselves on useragentstring.com. You'll notice that many user agent strings actually start with "Mozilla" (due to some wicked historical reasons), but simply testing to see if the string contains the browser's name (IE is "MSIE") should be enough.

问题是,在 HTTP_USER_AGENT 串是由浏览器报告,浏览器可能$他们希望p $ ptty多报告任何东西。歌剧甚至有一个内置选项,使其伪装自己作为IE或者FF。一般情况下,有一个很好的机会,浏览器嗅探基于用户代理字符串将最终错过,然后用户将恼火。我强烈建议你离开用户的一些方式来覆盖自动重定向。

Problem is, the HTTP_USER_AGENT string is reported by the browser, and the browser may pretty much report anything they want. Opera even has a built-in option to make it masquerade itself as IE or FF. Generally, there's a good chance that browser-sniffing based on the user agent string will eventually miss, and then the user will be annoyed. I'd strongly suggest you to leave the user some way to override the automatic redirection.

所以,这样的事情可能工作作为第一种方法:

So, something like this might work as a first approach:

RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} Opera
RewriteRule ^abcd.html$ opera.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} MSIE
RewriteRule ^abcd.html$ ie.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Chrome
RewriteRule ^abcd.html$ chrome.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Safari
RewriteRule ^abcd.html$ safari.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Firefox
RewriteRule ^abcd.html$ firefox.html [NC,L]

RewriteRule ^abcd.html$ default.html [L]

l标志可确保该规则是在传要执行的最后,因此,如果浏览器报告本身含有火狐,Safari,MSIE和Opera一个字符串,那么第一个规则来匹配(在这种情况下歌剧院)将确定着陆页。这个版本执行软重定向(浏览器的地址栏将不会改变)。如果你要硬,R = 301重定向,一定要拼出着陆页的完整的URL,即重写规则^ $ abcd.html http://example.com/xy/opera.html [NC,L,R = 301]

这篇关于htaccess的重定向到特定网页基于浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆