首页 > 解决方案 > Apache 根据用户代理提供不同的文件

问题描述

我的问题当用户从手机、台式机或平板电脑访问我的网站时,我想提供不同的 index.html 文件。我正在使用 Apache 并检查我是否可以基于用户代理来完成它。我编写了代码,但在某些情况下应用了错误的条件(例如,将显示带有 Mobile 的 Android 用户标题桌面版本)。Also in case of desktop everything works fine but when mobile or tablet is selected, I'll receive 404. Also I'm sure js/html code works in all cases I did test it locally.

我的问题是我用于条件的正则表达式是否正确?第一,第二,第三不起作用。你也知道为什么我在尝试访问手机或平板电脑时会收到 404 吗?

我的代码 httpd.conf

<IfModule dir_module>
    <If "%{HTTP_USER_AGENT} in { 'iPhone', 'Android' } and %{HTTP_USER_AGENT} =~ /Mobile/">
    DirectoryIndex mobile/index.html
    </If>
    <ElseIf "%{HTTP_USER_AGENT} in { 'BlackBerry', 'Opera Mini', 'Windows Phone' }">
    DirectoryIndex mobile/index.html
    </ElseIf>
    <ElseIf "%{HTTP_USER_AGENT} in { 'Tablet', 'Android' } and !%{HTTP_USER_AGENT} =~ /Mobile/">
    DirectoryIndex tablet/index.html
    </ElseIf>
    <ElseIf "%{HTTP_USER_AGENT} =~ /iPad/">
    DirectoryIndex tablet/index.html
    </ElseIf>
    <Else>
        DirectoryIndex desktop/index.html
    </Else> 
</IfModule>

文件结构

htdocs --- desktop
                - index.html
                - desktop.bundle.js
       --- mobile
                - index.html
                - mobile.bundle.js
       --- tablet 
                - index.html 
                - tablet.bundle.js

标签: regexapache

解决方案


推荐阅读