简体   繁体   English

在移动版的htaccess中重写规则

[英]Rewrite rule in htaccess for mobile version

I am using codeigniter framework for my site and I have done two site one for accessing through mobile and another for accessing through pcs. 我正在为我的网站使用codeigniter框架,我已经完成了两个站点一个用于通过移动访问,另一个用于通过pc访问。 both are in same domain . 两者都在同一个域中。 My site url is like this http://mydomain.com And my mobile is http://mydomain.com/celphone . 我的网站网址是这样的http://mydomain.com我的手机是http://mydomain.com/celphone I need to write htaccess rule which redirect to site url http://mydomain.com/celphone when i am accessing site from mobile and i need to rewrite to http://mydomain.com/ when i am accessing the site from pc. 我需要编写htaccess规则,当我从移动设备访问网站时,该规则会重定向到网站网址http://mydomain.com/celphone当我从PC访问网站时,我需要重写为http://mydomain.com/

How can i write rewrite rule in such case ,please help any help will be appreciated. 在这种情况下如何编写重写规则,请帮助任何帮助将不胜感激。

As scessor , said, it cannot be done with .htaccess . 作为 scessor ,说,它不能用 .htaccess完成。 You need to analyse the browser's user agent for this. 您需要为此分析浏览器的用户代理。

Check out this mobile detection in php. 在php中查看移动检测。

Usage is as easy as this 用法就像这样简单

if ($detect->isMobile()) {
    header("location: mobile.yourdomain.com");
    exit;
}

The .htaccess has no information about the device, so it can't recognize the difference. .htaccess没有关于设备的信息,因此无法识别差异。 You have to do this with your source code. 您必须使用源代码执行此操作。 (see my update below) (见下面的更新)

Eg in javascript analyze the browsers userAgent and redirect by setting window.location : 例如,在javascript中通过设置window.location分析浏览器userAgent和重定向:

<script type="text/javascript">// <![CDATA[  
    var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
    if (mobile) {  
        window.location = "http://www.yoursite.com/mobile.html";  
    }  
// ]]></script>

=== UPDATE === ===更新===

Read the anwers of this question to see how you can redirect in the .htaccess . 阅读此问题的答案,了解如何在.htaccess重定向。

Try doing it with PHP or any other server side language, you can look for the user agent and determine where to redirect the user. 尝试使用PHP或任何其他服务器端语言,您可以查找用户代理并确定重定向用户的位置。 You might also find a ready-made class to do the checking for you. 您可能还会找到一个现成的类来为您进行检查。 I've always used this and worked like a charm http://detectmobilebrowsers.mobi/ 我一直用这个,像一个魅力http://detectmobilebrowsers.mobi/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM