使用 .htaccess 重定向 IE 浏览器(远离 xhtml)

发布于 2024-09-30 04:17:26 字数 893 浏览 1 评论 0原文

正如标题所示,我想要使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-10-07 04:17:26

经过大量的碰运气(可能是因为我不知道.htaccess),我已经弄清楚了:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*MSIE.*
RedirectMatch ^/$ index.IE.htm
DirectoryIndex index.xhtml

当有人访问“http://www.yoursite.com/opticalFolder/”时,上面的代码片段将生效。如果访问者使用版本 4 以上的任何形式的 Internet Explorer,它将从那里重定向到index.IE.htm,否则(如果您不是 IE)转到index.xhtml。


我还发现了一个巧妙的技巧,可以让 Internet Explorer 实际上读取与 Text/HTML 相同的 .xhtml,这样您就不必维护同一站点的 2 个单独版本:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*MSIE.*
RewriteCond %{REQUEST_URI} \.xhtml$
RewriteRule .* - [T=text/html]

当有人访问“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:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*MSIE.*
RedirectMatch ^/$ index.IE.htm
DirectoryIndex index.xhtml

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:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*MSIE.*
RewriteCond %{REQUEST_URI} \.xhtml$
RewriteRule .* - [T=text/html]

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)

素染倾城色 2024-10-07 04:17:26

您可以使用此块将所有 IE 用户重定向到特殊页面(例如,您可以在其中放置警告消息)。

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (MSIE|Trident) [NC]
RewriteCond %{REQUEST_URI} !/unsupport-browser\.html [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|svg|png|gif|ico|css|js) [NC]
RewriteRule . /unsupport-browser.html [R=302,L]

You can use this block to redirect all IE users to a special page (you can for example put a warning message in it).

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (MSIE|Trident) [NC]
RewriteCond %{REQUEST_URI} !/unsupport-browser\.html [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|svg|png|gif|ico|css|js) [NC]
RewriteRule . /unsupport-browser.html [R=302,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文