【问题标题】:How to get the local path in RewriteCond?如何在 RewriteCond 中获取本地路径?
【发布时间】:2012-01-26 21:08:02
【问题描述】:

这是我的 .htaccess 文件:

RewriteEngine On

RewriteBase /projs/CodeIgniterTest

RewriteCond %{REQUEST_URI} !^/projs/CodeIgniterTest/index.php
RewriteRule ^(.*)$ index.php/$0

RewriteRule 工作正常,但我无法让RewriteCond 行与本地路径一起工作。我想让这个工作:

RewriteCond %{REQUEST_URI} !^index.php

但是,唉,%{REQUEST_URI} 不太关心RewriteBase 行,而是使用完整路径。有什么方法可以达到我想要的结果吗?我不想每次更改项目路径时都必须编辑 .htaccess 文件。

【问题讨论】:

    标签: php apache .htaccess apache2


    【解决方案1】:
    RewriteRule ^index.php - [L]
    RewriteRule ^(.*)$ index.php/$0
    

    或者只是

    RewriteCond $0 !^index.php
    RewriteRule ^(.*)$ index.php/$0
    

    【讨论】:

    • RewriteCond $0 !^index.php 工作就像一个魅力!但是你能解释一下$0 部分吗?我在 RewriteCond 文档中没有看到类似的内容
    • Apache 在执行 rewritecond 规则之前匹配重写规则中的正则表达式。因此,RewriteCond 可以在 TestString 中使用 $0..9。因此,在您的情况下, $0 包含相对于您的重写库的路径。 (它在httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond 的第一个要点中)
    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2013-08-02
    相关资源
    最近更新 更多