zoukankan      html  css  js  c++  java
  • apache 中 RewriteCond 介绍

    一、Yii2 URL美化

    修改Apache配置文件之前,需要先在 httpd.conf中搜索一下 rewrite ,查看一下 

    LoadModule rewrite_module modules/mod_rewrite.so 是否开启,如若没有,需要开启改模块,重写功能才能生效。

    Yii 美化URL,Apache 配置:

    <VirtualHost *:80>
        ServerName frontend.test  // 设置虚拟主机名
        DocumentRoot "/path/to/yii-application/frontend/web/"  // 虚拟主机的web根目录
        
        <Directory "/path/to/yii-application/frontend/web/">
            # use mod_rewrite for pretty URL support
            RewriteEngine on                                    // 打开重写引擎
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f                    // 如果请求的文件或目录不存在,
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php                                // 重写的规则失败请求转给 index.php 去处理
    
            # use index.php as index file
            DirectoryIndex index.php        // 默认的索引文件设为 index.php
    
            # ...other settings...
            # Apache 2.4
            Require all granted
            
            ## Apache 2.2
            # Order allow,deny
            # Allow from all
        </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName backend.test
        DocumentRoot "/path/to/yii-application/backend/web/"
        
        <Directory "/path/to/yii-application/backend/web/">
            # use mod_rewrite for pretty URL support
            RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php
    
            # use index.php as index file
            DirectoryIndex index.php
    
            # ...other settings...
            # Apache 2.4
            Require all granted
            
            ## Apache 2.2
            # Order allow,deny
            # Allow from all
        </Directory>
    </VirtualHost>

    然后在在 httpd.conf中搜索 DocumentRoot ,在它的后面粘贴上面的代码。

    详情请参考 yii 官方说明:https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md

    然后改一下hosts,因为 frontend.test  和 backend.test 并不是真实的域名

    在 hosts 文件中添加:

    127.0.0.1   frontend.test
    127.0.0.1   backend.test

    二、CI URL美化

    CI中默认情况下index.php 文件将被包含在你的URL中的,如果想要从url中删除这个默认的index.php,那么就需要用到Apache的强大的Rewrite功能了。

    CI官方文档中给出的解决办法是这样的:

    创建.htaccess 文件放到网站的根目录下,文件中的内容如下:

    1
    2
    RewriteCond $1 !^(index.php|images|robots.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]

    呵呵~~由于本人对Appache的这些规则也没怎么深入的研究过,于是在网上找了点关于这个appache的RewriteCond的一点资料,觉得写得不错很详细,现分享于大家:

    ———————————-小小地分割一下———————————————————
    Apache中 RewriteCond语句对于我来说一直是个难点,多次试图去把它搞明白,都没有结构,这次我终于算大概知道它的意思了。
    RewriteCond就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始、基础的功能,为了方便理解,下面来看看几个例子。

    1
    2
    3
    4
    5
    6
    7
    8
    RewriteEngine on
    RewriteCond  %{HTTP_USER_AGENT}  ^Mozilla/5.0.*
    RewriteRule  index.php            index.m.php
     
    RewriteCond  %{HTTP_USER_AGENT}  ^Lynx.*
    RewriteRule  index.php            index.L.php
     
    RewriteRule  index.php            index.b.php

    上 面语句的作用是当你是用FF浏览器访问index.php这个文件的时候,会自动让你访问到index.m.php这个文件,当你是用一些移动终端访问的 时候,会让你对index.php这个文件的访问实际访问的是index.L.php去,如果你是用其它的浏览器访问的时候,会让你跳到 index.b.php。在说形象一点,上面的语句就等同于程序里面的下面语句(依PHP语句为例):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    if($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0')
    {
    //跳转到对index.m.php的访问
    }
    else if($_SERVER['HTTP_USER_AGENT'] == 'Lynx')
    {
    //跳转到对index.L.php的访问
    }
    else
    //跳转到对index.b.php的访问

    再看例2:

    1
    2
    RewriteCond %{HTTP_REFERER} (www.test.cn)
    RewriteRule (.*)$ test.php

    上面语句的作用是如果你访问的上一个页面的主机地址是www.test.cn,则无论你当前访问的是哪个页面,都会跳转到对test.php的访问。

    再看例三:

    1
    2
    3
    4
    RewriteCond %{REMOTE_HOST} ^host1.* [OR]
    RewriteCond %{REMOTE_HOST} ^host2.* [OR]
    RewriteCond %{REMOTE_HOST} ^host3.*
    RewriteRule (.*)$ test.php

    上面语句的作用是如果你的地址是host1或host2或host3的时候,则就跳到对test.php。从这里可以看出,RewriteCond语句之间默认的是AND,如果想要OR,则要明确的写出来。

    下面是自己收藏的一些有用的重写规则:

    1
    2
    3
    4
    5
    RewriteCond %{REQUEST_FILENAME} !-f   //如果文件存在,就直接访问文件,不进行下面的RewriteRule.(不是文件或文件不存在就执行重写)
     
    RewriteCond %{REQUEST_FILENAME} !-d   //#如果目录存在就直接访问目录不进行RewriteRule
     
    RewriteCond %{REQUEST_URI} !^.*(.css|.js|.gif|.png|.jpg|.jpeg)$ //#如果是这些后缀的文件,就直接访问文件,不进行Rewrite
    <IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
    </IfModule>
    

    1、排除一些条件,必须两个条件都满足后才重定向到index.php
    //如果你访问的文件不等于目录

    RewriteCond %{REQUEST_FILENAME} !-d
    

    //如果你访问不是文件,比如你可能访问的JPEG等图片文件

    RewriteCond %{REQUEST_FILENAME} !-f
    
    

    2、^(.*)$ 匹配所有的路径映射到入口文件 index.php/$1

    3、标签 [QSA,PT,L] QSA:表示保留参数如get传值?xxx==xx...; PT:再把这个URL交给Apache处理;L:作为最后一条;
    PT和L可加可不加。

    ———————————-小小地分割一下———————————————————
    本文摘自: http://hi.baidu.com/%D2%BB%CE%C4%CC%EC/blog/item/ace7f14e19851cc4d0c86af2.html

  • 相关阅读:
    hotmail 收不到邮件的问题
    getaddrinfo 报错 Invalid value for ai_flags
    Avoiding Common Networking Mistakes
    关掉标准输出
    不需要 root 权限的 ping
    select 的问题
    Behavior Tree 用 Lua 实现一个最简行为树
    对 UDP 的一些思考
    Windows UDP sockets: recvfrom() fails with error 10054
    和等于某个数的所有组合
  • 原文地址:https://www.cnblogs.com/chrdai/p/6954327.html
Copyright © 2011-2022 走看看