使用 .htaccess 重定向 IE 浏览器(远离 xhtml)
正如标题所示,我想要使用xhtml作为我的主页,并且我想将使用 Internet Explorer 浏览器的访问者重定向到不同的页面(内容较少的 .htm说“你正在使用 IE,坚强的奶子。”)。
由于 IE 自动下载 app/xhtml 文件,我无法使用 javascript 等来执行此操作,所以我认为唯一的选择是使用 .htaccess。但是在用谷歌搜索类似帖子中的 htaccess 示例近 2 小时后,我仍然无法让它工作,我在 IE 和 chrome/firerfox 中都得到 403 或 500 。
这是我最后尝试的:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} "MSIE 5" [OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 6" [OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 7" [OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 8"
RewriteRule ^(.*)$ http://www.mypage/index.IE.htm
</IfModule>
无论如何,要使更清楚的是,我希望我的 .htaccess 能够执行以下操作:
if(whoever_accesses_my_page is anyVersionOf_IE)
set_index("http://www.mypage/index.IE.htm");
else
set_index("http://www.mypage/index.xhtml");
as the title says, I want to use an xhtml as my homepage and I want to redirect visitors using Internet Explorer browsers to a different page (a .htm with less content that says "You're using IE, tough tittie.").
Since IE automatically downloads app/xhtml files, I can't do this using javascript and whatnot, so I think the only option is to use .htaccess. But after almost 2 hours of googling htaccess examples from similar posts, I still can't get it to work, I get either 403 or 500 both in IE and in chrome/firerfox..
Here's what I tried last:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} "MSIE 5" [OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 6" [OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 7" [OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 8"
RewriteRule ^(.*)$ http://www.mypage/index.IE.htm
</IfModule>
Anyway, to to make this clearer, I'd like my .htaccess to do this:
if(whoever_accesses_my_page is anyVersionOf_IE)
set_index("http://www.mypage/index.IE.htm");
else
set_index("http://www.mypage/index.xhtml");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过大量的碰运气(可能是因为我不知道.htaccess),我已经弄清楚了:
当有人访问“http://www.yoursite.com/opticalFolder/”时,上面的代码片段将生效。如果访问者使用版本 4 以上的任何形式的 Internet Explorer,它将从那里重定向到index.IE.htm,否则(如果您不是 IE)转到index.xhtml。
我还发现了一个巧妙的技巧,可以让 Internet Explorer 实际上读取与 Text/HTML 相同的 .xhtml,这样您就不必维护同一站点的 2 个单独版本:
当有人访问“http:// /www.yoursite.com/anyPageName.xhtml”。如果是 Internet Explorer,则会将文档类型覆盖为 text/html,而不是 application/xhtml+xml。否则,任何其他浏览器都会正常打开 xhtml。
问题:如何让代码段 #2 适用于仅包含目录路径的 URL?
(它仅适用于“http://www.yoursite.com/pageName.xhtml”;我怎样才能让它适用于“http://www.yoursite.com/optionalFolder/” ?)
换句话说,
DirectoryIndex index.xhtml
在代码片段 #2 中不起作用(它超越了 IE 的 RewriteRule;如果我添加 DirectoryIndex,IE 将自动下载 xhml)After A LOT of hit and miss (probably since I don't know .htaccess) I've figured it out:
The above snippet will have effect when someone visits "http://www.yoursite.com/optionalFolder/". It will redirect from there, to index.IE.htm if the visitor is any form of Internet Explorer over version 4, else (if you're not IE) go to index.xhtml.
I have also found a neat trick to get Internet Explorer to actually read the same .xhtml as Text/HTML, so you won't have to maintain 2 separate versions of the same site:
This snippet has effect when someone accesses "http://www.yoursite.com/anyPageName.xhtml". If it's Internet Explorer, then it will overwrite the document type to text/html instead of application/xhtml+xml. Else, any other browser will open the xhtml normally.
Question: How can I get snippet #2 to work for a URL that has just the directory path?
(it only works for "http://www.yoursite.com/pageName.xhtml"; how can I get it to work for "http://www.yoursite.com/optionalFolder/"?)
In other words,
DirectoryIndex index.xhtml
doesn't work in snippet #2 (it overpowers the RewriteRule for IE; if I add DirectoryIndex, IE will auto-download the xhml)您可以使用此块将所有 IE 用户重定向到特殊页面(例如,您可以在其中放置警告消息)。
You can use this block to redirect all IE users to a special page (you can for example put a warning message in it).