【问题标题】:Access folder of parent website from mobile website after htaccess redirecthtaccess重定向后从移动网站访问父网站的文件夹
【发布时间】:2015-05-21 09:05:12
【问题描述】:

通过移动设备访问时,我将我的网站重定向到移动网站。

这是 htaccess 代码:

RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|iphone [NC]
RewriteRule ^ http://m.example.com%{REQUEST_URI} [R,L]

FTP的文件夹结构是这样的:

Images
ProImages
Mobile // All mobile site data into this

当我尝试从我的移动网站访问http://www.example.com/ProImages/abc.jpg 时,它不会显示,因为它一旦尝试调用www,就会重定向到m

我尝试使用../ProImages,但同样没有解决问题。

有人可以帮忙吗?

【问题讨论】:

  • 这是正确的行为,因为您的规则基于HTTP_USER_AGENT 进行重定向。为防止重定向,您可以使用此 URL:ttp://www.example.com/ProImages/abc.jpg?desktop

标签: php .htaccess


【解决方案1】:

您可以允许访问移动版本的图像文件夹

RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|iphone [NC]
RewriteCond %{REQUEST_URI} !^/ProImages/ [NC]
RewriteRule ^ http://m.example.com%{REQUEST_URI} [R,L]

【讨论】:

  • 好吧,这似乎是对我问题的合理回答。由于这是我唯一关心的文件夹,它可以很好地工作。谢谢:)
【解决方案2】:

您可以从 Mobile 文件夹中创建指向您的图像文件夹的符号链接。

如果您有 shell 访问权限,请在 Mobile 文件夹中执行以下操作:

ln -s /path/to/document/root/ProImages ./ProImages

这样你就可以调用Mobile/ProImages,显示的内容来自父文件夹的ProImages

【讨论】:

  • 不错的解决方案,也会看看这个。 :)
【解决方案3】:

通常我都使用服务器进行重定向,但使用 PHP 进行移动。我建议使用 mobile_detect 类。我的工作非常好,它几乎可以匹配所有内容。您甚至可以根据具体情况进行检测,例如是否在平板电脑或 ipad 上或任何您想要的东西上。它非常易于使用,而不是使用 .htaccess 来做用户代理。所以你可以像下面这样做一个 if 语句,然后用 php 做一个重定向。

这是他们页面的代码示例。 http://mobiledetect.net/

// Include and instantiate the class.
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;

// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
  header('Location: http://m.example.com/'.$_SERVER["REQUEST_URI"]);
}

// Any tablet device.
if( $detect->isTablet() ){

}

// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){

}

// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){

}

if( $detect->isAndroidOS() ){

}

// Alternative method is() for checking specific properties.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->is('Chrome')
$detect->is('iOS')
$detect->is('UC Browser')
// [...]

// Batch mode using setUserAgent():
$userAgents = array(
'Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
'BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103',
// [...]
);
foreach($userAgents as $userAgent){

  $detect->setUserAgent($userAgent);
  $isMobile = $detect->isMobile();
  $isTablet = $detect->isTablet();
  // Use the force however you want.

}

// Get the version() of components.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->version('iPad'); // 4.3 (float)
$detect->version('iPhone') // 3.1 (float)
$detect->version('Android'); // 2.1 (float)
$detect->version('Opera Mini'); // 5.0 (float)
// [...]

【讨论】:

  • header('Location: http://m.example.com/'.$_SERVER["REQUEST_URI"].'); 末尾缺少一个撇号
  • @Lupin 哎呀,我写得很快。 :) 已修复。
  • 我以前使用过这个,但是 htaceess 的方式要好得多。感谢您的信息:)
猜你喜欢
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
相关资源
最近更新 更多