如何使用.htaccess在手机上显示移动网站但不是平板电脑?

时间:2014-01-10 22:50:20

标签: .htaccess redirect

我们有一个桌面和移动网站。但是,移动版本非常简单,在iPad /平板电脑上看起来不太好。我们是否可以禁止移动网站出现在平板电脑上,并且仅将其显示在手机设备上?这是一个apache / PHP / Drupal站点。我对.htaccess很新。

<FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$">
  Order allow,deny
</FilesMatch>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php index.html index.htm

# Override PHP settings that cannot be changed at runtime. See
# sites/default/default.settings.php and drupal_environment_initialize() in
# includes/bootstrap.inc for settings that can be changed at runtime.

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
  php_flag magic_quotes_gpc                 off
  php_flag magic_quotes_sybase              off
  php_flag register_globals                 off
  php_flag session.auto_start               off
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_flag mbstring.encoding_translation    off
</IfModule>

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A1209600

  <FilesMatch \.php$>
    # Do not allow PHP scripts to be cached unless they explicitly send cache
    # headers themselves. Otherwise all scripts would have to overwrite the
    # headers set by mod_expires if they want another caching behavior. This may
    # fail if an error occurs early in the bootstrap process, and it may cause
    # problems if a non-Drupal PHP file is installed in a subdirectory.
    ExpiresActive Off
  </FilesMatch>
</IfModule>

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # Block access to "hidden" directories whose names begin with a period. This
  # includes directories used by version control systems such as Subversion or
  # Git to store control files. Files whose names begin with a period, as well
  # as the control files used by CVS, are protected by the FilesMatch directive
  # above.
  #
  # NOTE: This only works when mod_rewrite is loaded. Without mod_rewrite, it is
  # not possible to block access to entire directories from .htaccess, because
  # <DirectoryMatch> is not allowed here.
  #
  # If you do not have mod_rewrite installed, you should remove these
  # directories from your webroot or otherwise protect them from being
  # downloaded.
  RewriteRule "(^|/)\." - [F]

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # uncomment the following:
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # Handle traffic to /mobile by code within the theme
  RewriteRule ^mobile(.*)$ /sites/all/themes/custom/mobile_site$1 [L]

1 个答案:

答案 0 :(得分:1)

我意识到这并没有像你提出的那样完全回答这个问题,但我会提出一个非常不同的,非常有效的解决方案。当您的站点首次被点击并使用重定向时,请使用移动检测类/库作为挂钩。在Github上有一个名为Mobile-Detect的优秀类,它维护得非常好,并且易于实现:https://github.com/serbanghita/Mobile-Detect/

您应该能够将其作为库构建到您的站点中,然后设置将持久保存用户设备状态的cookie或会话,以便不必在每个页面请求中检测到它。

我建议这样做是因为如果你在Apache虚拟主机或.htaccess文件中执行此操作,则需要执行以下操作:

    RewriteEngine on
    RewriteCond %{HTTP_USER_AGENT} iphone|ipad|android|blackberry [NC]
    RewriteRule ^$ /mobile.php

很简单,但您现在必须为任何移动设备/平板电脑用户代理保持这一点。什么更大的Android屏幕?您的应用程序中内置的移动检测类将允许您更新而不会弄乱您的Apache规则,并且您将从社区保持最新的东西中受益。并且像Apache这样评估每个请求只会占用大量资源,可以为每个访问者执行一次,然后通过cookie或会话进行处理。