zoukankan      html  css  js  c++  java
  • PHP扩展功能 ---- 伪静态

    一、入门三部曲

    1、什么是伪静态?

    改写URL,以静态的url形式访问页面,但其实是用PHP一类的动态脚本来处理的。

    2、为什么要用伪静态?

    需要动态获取数据,但是又希望能够对搜索引擎友好。

    3、怎么用伪静态?

    (1)隐藏入口文件
    (2)将url参数形式中的?、&、=进行转换

    如: http://localhost/index.php?m=home&c=user&a=login改写成http://localhost/home/user/login.html的形式
    更多的参数即作为get传递的额外参数

    二、伪静态在PHP中的应用

    1、隐藏入口文件

    (1)修改apache配置文件

    • 开启rewrite_module功能
    LoadModule rewrite_module modules/mod_rewrite.so
    

    (2)允许重启

    <Directory "${INSTALL_DIR}/www/">
        Options +Indexes +FollowSymLinks +Multiviews
        AllowOverride all
        Require local
    </Directory>
    

    (3)在index.php的同级目录下创建.htaccess文件

    <IfModule mod_rewrite.c>
      Options +FollowSymlinks -Multiviews
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
    </IfModule>
    

    2、参数优化

    访问优化后的url时,需要将路由信息转换为模块、控制器、方法(操作)名。

    参数个数大于3时,后面的参数作为params数组

    // http://localhost/home/user/login.html 以此url为例
    //$_SERVER['PATH_INFO'] => /home/user/login.html
    
    $suffix = strrchr($_SERVER['PATH_INFO'], '.');  // => .html
    
    $pathInfo = str_replace($suffix,'',$_SERVER['PATH_INFO']); // => /home/user/login
    
    $path = substr($pathInfo, 1); // => home/user/login
    
    $arr = explode('/'. $path); //['home','user','login']
    
    • strrchr(string $haystack , mixed $needle): 查找指定字符$needle在字符串$haystack中的最后一次出现
    • str_replace( mixed $search , mixed $replace , mixed $subject [, int &$count ] ): 返回将$subject中全部的$search都用$replace替换之后的字符串或数组。
    • substr( string $string , int $start [, int $length ]): 返回字符串 $string$start$length 参数指定的子字符串。
    • explode(string $delimiter , string $string [, int $limit ]): 此函数返回由字符串组成的数组,每个元素都是 $string 的一个子串,它们被字符串 $delimiter 作为边界点分割出来。
  • 相关阅读:
    js类型自动转换以及==对比规则
    js改变作用域链
    cookie简单实例
    js操作cookie
    body设置margin为0
    inline-block和block元素水平居中显示
    执行git clone遇到警告解决办法
    git中各大写字母表示含义
    git命令报错
    linux.txt
  • 原文地址:https://www.cnblogs.com/zqunor/p/8672831.html
Copyright © 2011-2022 走看看