【问题标题】:Problems with configuring Apache for mobile website为移动网站配置 Apache 的问题
【发布时间】:2012-05-22 07:01:28
【问题描述】:

我在为一个同时具有普通版和移动版的网站配置 Apache 时遇到了一些问题。这个想法是使用移动浏览器将用户自动重定向到移动版本,如果普通台式电脑上的某人试图连接到移动版本,则将其重定向到普通网站。

现在它将移动用户正确重定向到移动网站。但它似乎无法以其他方式重定向到桌面版本。此外,当您访问网站的移动版本时,它会显示默认的“It works!”出于某种原因页面而不是移动索引页面..

这是我使用的所有配置。希望有人能够帮助我解决这个问题。提前致谢! 普通网站:

<VirtualHost *:80>
ServerAdmin webmaster@henal.local
ServerName henal.local
ServerAlias www.henal.local

DocumentRoot /var/www/henal.local
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

<Directory /var/www/henal.local>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    DirectoryIndex index.html
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/deltionkrant/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug

CustomLog ${APACHE_LOG_DIR}/deltionkrant/access.log combined

     Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

 </VirtualHost>

.htaccess 普通网站:

RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-  mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
    RewriteRule ^$ http://m.henal.local/ [L,R=302]

移动网站:

<VirtualHost *:80>
ServerAdmin webmaster@henal.local
ServerName m.henal.local
ServerAlias henal.local

DocumentRoot /var/www/henal.local/mobile
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

<Directory /var/www/henal.local/mobile>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    DirectoryIndex index.html
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

     Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

.htaccess 移动网站:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|googlebot- mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^$ http://www.henal.local/ [L,R=302]

【问题讨论】:

    标签: linux apache mobile web


    【解决方案1】:

    好的 - 所以您正在尝试在此处进行内容协商,并且您正在用户代理上进行正则表达式。除了您拥有的重写规则之外,您还需要考虑以下几点:

    1. 使用Apache Mobile Filter 而不是正则表达式。它更准确并提供更广泛的功能
    2. 您应该允许用户选择他们想要的表示/体验。因此,如果您向他们展示移动版本,那么您应该让他们选择桌面版本,因为这实际上可能是他们想要的;反之亦然。
    3. 这可能有多种原因,例如他们有桌面书签,有人分享了桌面版本的 url 等等。您的内容协商技术非常基础,没有考虑任何这些用例.

    考虑到这一点,具体到您的上述问题:

    尝试从 RewriteCond 模式中删除引号并添加 R=302 和 L 标志,以确保现在执行其他重写指令。即:

    RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|googlebot- mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos) [NC,R=302,L]
    

    其次,您可能会看到默认桌面“它可以工作”,因为您正在桌面浏览器上进行测试并被重定向到桌面主机。

    【讨论】:

    • 感谢您的回复!这主要是学校演示,因此不会用于公共网站(尽管我可能会根据您的提示为我的真实公共网站创建更好的解决方案!)。这也是我这样配置它的原因。真的很感谢你的建议,并将把这些点用于未来的项目。 :)
    • 但我尝试删除引号并将 2 个标志添加到 .htaccess。我再次测试了它,但它没有任何区别。关于“它可以工作”页面,是的,我正在使用我在(虚拟 Ubuntu)上配置 Apache 的机器上的浏览器对其进行测试。我编辑了主机文件,以便 henal.local 和 m.henal.local 引用 127.0.0.1 所以也许这就是原因?虽然正常的网站似乎工作得很好..
    • ok,所以你需要用手机浏览器测试。通过上述配置,如果您在桌面浏览器中输入 m.henal.local,那么它将重定向到 henal.local。由于您的 DNS 无法从外部解析,因此您将无法使用手机进行测试。相反,为什么不试试 Android 模拟器:developer.android.com/guide/developing/devices/emulator.html
    • p.s +1 用于学校演示
    【解决方案2】:

    略微切线,回应威廉格林利的 “既然你的DNS不是外部解析的,那你就不能用手机测试了。”

    仅供参考,Pagekite (http://pagekite.net/) 可以轻松地将 localhost 暴露在网络上,以便通过手机在开发机器上进行测试。

    外面有人。

    【讨论】:

      猜你喜欢
      • 2013-01-31
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2011-08-29
      相关资源
      最近更新 更多