phpcms v9 实现pc端,移动端共用数据库方法总结

本文介绍了如何利用phpcmsv9在同一系统下创建电脑站和移动站,实现内容同步和网址对应。通过绑定多个网址,设置伪静态规则,并修改PHP代码来根据域名头部判断调用相应模板。此外,还提供了在PC端模板中添加跳转脚本来自动检测移动设备并重定向至移动站的方法。
摘要由CSDN通过智能技术生成

下面说说phpcms v9同一系统如何利用双模板制作电脑站及移动站,本文参考网上资料加以整理。最后得出功能是PC站与移动站共用一个数据库,做到内容发布同步,通过伪原创得出PC站网址与移动站网址一一对应,比如如:http://www.66fww.com对http://m.66fww.com,只是网址头部不同,并且通过判断用手机浏览网站时自动跳动到移动站网址及模板,每个网址一一对应跳转。表达能力有限,越说我都越糊涂了,下面一点点修改吧

第一步:网址解析

绑定三个网址:http://www.XX.com、http://m.XX.com、http://XX.com。

第二步:做网址伪静态处理

栏目伪原创后网址如:http://www.XX.com/list-1-1.html,内容页网址:http://www.XX.com/show-1-1-1.html。对应移动站网址:栏目伪原创后网址如:http://m.XX.com/list-1-1.html,内容页网址:http://m.XX.com/show-1-1-1.html。

1、apache  伪静态写法(部分写法,可根据实际修改)

RewriteEngine on
RewriteRule ^content-([0-9]+)-([0-9]+)-([0-9]+).html index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3
RewriteRule ^show-([0-9]+)-([0-9]+)-([0-9]+).html index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3
RewriteRule ^list-([0-9]+)-([0-9]+).html index.php?m=content&c=index&a=lists&catid=$1&page=$2

RewriteRule ^zt/         index.php?m=content&c=index&a=lists&catid=165
RewriteRule ^zt-([0-9]+)/   index.php?m=content&c=index&a=lists&catid=165&page=$1

2、Nginx  伪静态写法(部分写法,可根据实际修改)

location / {
	rewrite ^/content-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3;
	rewrite ^/show-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3;
	rewrite ^/list-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=lists&catid=$1&page=$2;
	rewrite ^/soft/([0-9]+).html /index.php?m=content&c=index&a=show&catid=10&id=$1;
rewrite ^/zt/list-([0-9]+)/ /index.php?m=content&c=index&a=lists&catid=$1;
	rewrite ^/zt/list-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=lists&catid=$1&page=$2;
	rewrite ^/tags-(.*)-([0-9]+).html /index.php?m=content&c=tag&a=lists&tag=$1&page=$2;
	rewrite ^/download/(.*)/ /index.php?m=content&c=down&a_k=$1 ;
	rewrite ^/zt/ /index.php?m=content&c=index&a=lists&catid=165;
	rewrite ^/zt-([0-9]+)/ /index.php?m=content&c=index&a=lists&catid=165&page=$1;
	
	}

第三步:修改phpcms/modules/content/index.php。

在31行处找到include template('content','index',$default_style);修改为:

if(substr($_SERVER['SERVER_NAME'], 0,1) == 'm'){

include template('content_m','index',$default_style);

}else{

include template('content','index',$default_style);

}

分别在203、265、278行处把include template('content',$template);修改为:

if(substr($_SERVER['SERVER_NAME'], 0,1) == 'm'){

include template('content_m',$template);

}else{

include template('content',$template);

}

意思是,当域名头部为m时选择 content_m文件夹里的模板,www时选择content文件夹的模板。

注意:如果是Nginx 服务器  

$_SERVER['SERVER_NAME']  要换成  $_SERVER['HTTP_HOST], 否则失效

第四步:

把存放模板的文件夹content复制一份,重命名为content_m,里面的模板文件名一样,里面基本文件header.html,footer.html,index.html,category.html,list.html,show.html。content文件夹内模板为PC板调用,content_m为移动站调用,移动板文件建议用html5制作。

第五步:在PC板模板加入跳转脚本

判断为移动端自动跳转网址

脚本写法:<script src="{JS_PATH}uaredirect.js" type="text/javascript"></script>

<script type="text/javascript">uaredirect("要跳转到的移动站网址");</script>

首页、单页、栏目页、内容页各自调用单独的头部模板,方便写跳转网址。

1、

首页跳转的网址写法为:<script type="text/javascript">uaredirect("http://m.XX.com/index.php");</script>

2、

单页、栏目页跳转的网址写法为:<script type="text/javascript">uaredirect("http://m.XX.com/list-{$catid}-{$page}.html");</script>

3、内容页跳转的网址写法为:<script type="text/javascript">uaredirect("http://m.XX.com/show-{$catid}-{$id}-{$page}.html");</script>

脚本uaredirect.js的文本为:

function uaredirect(f){
    try{
        if(document.getElementById("bdmark")!=null){
            return
        }var b=false;if(arguments[
            1
        ]){
            vare=window.location.host;var a=window.location.href;if(isSubdomain(arguments[
                1
            ],
            e)==1){
                f=f+"/#m/"+a;b=true
            }else{
                if(isSubdomain(arguments[
                    1
                ],
                e)==2){
                    f=f+"/#m/"+a;b=true
                }else{
                    f=a;b=false
                }
            }
        }else{
            b=true
        }if(b){
            var c=window.location.hash;if(!c.match("fromapp")){
                if((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))){
                    location.replace(f)
                }
            }
        }
    }catch(d){
        
    }
}
function isSubdomain(c,d){
    this.getdomain=function(f){
        vare=f.indexOf("://");if(e>0){
            varh=f.substr(e+3)
        }else{
            var h=f
        }varg=/^www\./;if(g.test(h)){
            h=h.substr(4)
        }returnh
    };if(c==d){
        return1
    }else{
        var c=this.getdomain(c);varb=this.getdomain(d);if(c==b){
            return1
        }else{
            c=c.replace(".",
            "\\.");vara=new RegExp("\\."+c+"$");if(b.match(a)){
                return2
            }else{
                return0
            }
        }
    }
};

个人建议脚本尽量放在靠前位置比较好吧,因为加载网页时尽早触发脚本,跳转到移动网页会不会快点呢,这是我的个人意见,不知对否请高手指正。

第六步:content_m中模板调用的更改

由于phpcms把文章的url都固定写死在数据表中.所以,页面中的标签不能在使用{$r[url]},而要改成{str_replace('http://www.','http://m.',$r[url])},意思是,截取url,把http://www.替换成http://m.,其它琏链接的标签参考此法。

PC版首页可以生成静态,也可以是动态。双模板伪原创好处是写百度适配非常简单,直接三句:

第2页

http://www.xx.com对http://m.xx.com

http://www.xx.com/list-(\d+)-(\d+).html对http://m.xx.com/list-${1}-${2}.html

http://www.xx.com/show-(\d+)-(\d+)-(\d+).html对http://m.xx.com/show-${1}-${2}-${3}.html

当然PC站静态也可以的,不过移动站就不能静态或伪静态了,因为只能生成一个静态页面,起码我不会弄,但可以跳转到移动站的动态网址,跳转网址可以参考伪静态的,无非是三个参数的对应。好了,这是我用双模板制作PC手移动站的一点点心得,完胜系统自带的手机门户站吧,手机站和PC站一样的标签调用。如有看不懂的地方可以提问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值