zoukankan      html  css  js  c++  java
  • apache php 开启伪静态

    打开apache的配置文件httpd.conf
    找到
    #LoadModule rewrite_module modules/mod_rewrite.so
    把前面#去掉。没有则添加,但必选独占一行,使apache支持 mod_rewrite 模块
    
    找到
    <Directory "D:/ApacheServer/web">
        #
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs/2.2/mod/core.html#options
        # for more information.
        #
        Options Indexes FollowSymLinks
    
        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   Options FileInfo AuthConfig Limit
        #
        AllowOverride None
    
        #
        # Controls who can get stuff from this server.
        #
        Order allow,deny
        Allow from all
    
    </Directory>
    把
    AllowOverride None 换成 AllowOverride All 使apache支持 .htaccess 文件
    
    重启apache服务器
    
    在要启用伪静态的 PHP 项目根目录下建立 .htaccess 文件
    
    在 .htaccess 文件中输入内容
    
    <IfModule mod_rewrite.c>
        Options +FollowSymlinks -Multiviews
        RewriteEngine on
        #本地测试的例子
        RewriteRule index.html$ index.php
        RewriteRule index-([1-9]+[0-9]*).html$ index.php?p=$1
        RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
        
        
        #再附几个常用的规则内容
        #访问index.html导向访问index.php?page=index
        RewriteRule index.html$ index.php?page=index [L]
    
    
        #禁止访问git文件夹
        RewriteRule ^.git - [F,L]
    
    
        #301重定向,自动在网址后加反斜杠 / ,文件路径除外(会先访问该文件,找不到则在文件路径后也加 /)
        #RewriteBase / 是设置了重写的基准为该域名的根目录,写了这个的话,RewriteRule . index.php 就可以了,但是没写的话,就要多一个斜杠了RewriteRule . /index.php
        RewriteBase /
        #如果不是文件,才会执行下一条RewriteRule。
        RewriteCond %{REQUEST_FILENAME} !-f
        #如果不是目录,才会执行下一条RewriteRule
        #RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !(.*)/$
        RewriteRule ^(.*)$ $1/ [L,R=301]
        
        #当访问products/mobile-phones/gdshdfh/或者products/tablets/gdshdfh/或者products/wear/gdshdfh/ 等的时候都指向同一路径 products/overview/ename/$2/,gdshdfh这里为乱想,匹配正则2的公式[a-zA-Z0-9-]{1,}即可
        RewriteRule products/(mobile-phones|tablets|wear|home-internet-media|accessories)/([a-zA-Z0-9-]{1,})/$ products/overview/ename/$2/ [QSA,PT,L]
        #伪静态也有缓存,修改规则后要清缓存
    </IfModule>
    
    注释:
    RewriteEngine   为重写引擎开关,on为开启,off为关闭。
    
    RewriteRule     是路由转向规则,$ 之前路径为浏览器中要输入路径,这里可以用正则表达式表达。$+空格 后路径为后台实际转向路径,
    转向后台实际路径时可以传参数,例子里的后台页面可以用$_GET['p']   $_GET['action']  $_GET['id'] 来接收
    $1 代表浏览器路径中输入的第一个正则表达式的值,以此类推,$2代表第二个正则表达式的值
    RewriteRule 路由转向规则里正则表达式用括号 () 括起来
    
    RewriteRule 后面中括号里的大写字符注释
    NC  : no case,就是说不区分大小写
    R   : redirect,重定向
    F   : forbidden,禁止访问,强制当前URL为被禁止的,即,立即反馈一个HTTP响应代码403(被禁止的)。 使用这个标记,可以链接若干RewriteConds以有条件地阻塞某些URL
    L   : last,表示已经是最后一条规则,立即停止重写操作,并不再应用其他重写规则。
    QSA : qsappend,此标记强制重写引擎在已有的替换串中追加一个请求串,而不是简单的替换。 如果需要通过重写规则在请求串中增加信息,就可以使用这个标记。
    PT  : passthrough,此标记强制重写引擎将内部结构request_rec中的uri字段设置为 filename字段的值,它只是一个小修改,使之能对来自其他URI到文件名翻译器的 Alias,ScriptAlias, Redirect 等指令的输出进行后续处理。举一个能说明其含义的例子: 如果要通过mod_rewrite的重写引擎重写/abc为/def, 然后通过mod_alias使/def转变为/ghi,
                可以这样: RewriteRule ^/abc(.*) /def$1 [PT]
          Alias /def /ghi
                如果省略了PT标记,虽然mod_rewrite运作正常, 即, 作为一个使用API的URI到文件名翻译器, 它可以重写uri=/abc/…为filename=/def/…, 但是,后续的mod_alias在试图作URI到文件名的翻译时,则会失效。
                注意: 如果需要混合使用不同的包含URI到文件名翻译器的模块时, 就必须使用这个标记。混合使用mod_alias和mod_rewrite就是个典型的例子。
          For Apache hackers
                如果当前Apache API除了URI到文件名hook之外,还有一个文件名到文件名的hook, 就不需要这个标记了! 但是,如果没有这样一个hook,则此标记是唯一的解决方案。 Apache Group讨论过这个问题,并在Apache 2.0 版本中会增加这样一个hook。
    
    +FollowSymlinks : 打开FollowSymlinks项,某些服务器配置中,mod_rewrite要求有followsymlinks,否则会显示500内部服务器错误,如果没有指定FollowSymLinks的选项(即Options FollowSymLinks),或者指定了 SymLinksIfOwnerMatch选项,Apache将不得不调用额外的系统函数来检查符号链接。每次针对文件名的请求都将触发一次检查
    
    -Multiviews     : 关闭Multiviews,MultiViews的效果是:如果服务器收到对/some/dir/foo的请求,而/some/dir/foo并不存在,但是如果/some/dir 启用了MultiViews ,则服务器会查找这个目录下所有的foo.* 文件,并有效地伪造一个说明这些foo.* 文件的类型表,分配给他们相同的媒体类型及内容编码,并选择其中最合适的匹配返回给客户
    
    
    
    例子所在项目为test
    在项目下 index.php 页面内写入内容
    <?php
    if ($_GET ['p']) {
        echo "p : " . $_GET ['p'];
    }
    
    if ($_GET ['action']) {
        echo "action : " . $_GET ['action'];
    }
    
    if ($_GET ['id']) {
        echo "id : " . $_GET ['id'];
    }
    ?>
    
    在浏览器中输入
    http://localhost/test/index.html
    http://localhost/test/index-99.html
    http://localhost/test/page-18.html
    
    都会转向 http://localhost/test/index.php 页面
    并且依次
    http://localhost/test/index.html     页面什么都不显示
    http://localhost/test/index-99.html  页面显示 p : 99
    http://localhost/test/page-18.html   页面显示 action : pageid : 18
  • 相关阅读:
    ubuntu安装jdk的两种方法
    LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)
    LeetCode 617. Merge Two Binary Tree (合并两个二叉树)
    LeetCode 476. Number Complement (数的补数)
    LeetCode 575. Distribute Candies (发糖果)
    LeetCode 461. Hamming Distance (汉明距离)
    LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
    LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
    LeetCode 371. Sum of Two Integers (两数之和)
    LeetCode 342. Power of Four (4的次方)
  • 原文地址:https://www.cnblogs.com/dreamhome/p/3204165.html
Copyright © 2011-2022 走看看