httpd.conf中的apache mod_rewrite规则用于修改某些路径,但不修改其他路径

时间:2010-05-04 06:34:13

标签: codeigniter apache-config

我在Fedora 10上为Apache / 2.2.14创建适当的重写规则时遇到了很大的挑战。我正在使用。{3}},它使用.htaccess文件。 (从CodeIgniter网址中搜索删除“index.php”大约10%。)但是因为不建议用于生产服务器,我正在尝试调整它以在/ etc / httpd /中工作CONF / httpd.conf中。

<VirtualHost *:80>
    ServerName ci_doctrine
    DocumentRoot /var/www/html/ci_doctrine
    ErrorLog /var/log/httpd/cid-error_log
    CustomLog /var/log/httpd/cid-access_log common
        <IfModule mod_rewrite.c>
                RewriteEngine on
                RewriteLog /var/log/httpd/cid_rewrite
                RewriteLogLevel 9

#               RewriteCond ^/css/style.css$  (these have bad syntax, but that's beside the point)
#               RewriteRule ^/css/style.css$ /css/style.css [L]

                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule ^(.*)$ /index.php/$1 [L]
        </IfModule>
        <IfModule !mod_rewrite.c>
                ErrorDocument 404 /ci_doctrine/index.php
        </IfModule>
</VirtualHost>

似乎教程.htaccess规则正确测试现有文件,然后在这种情况下不改变URL,但重写日志说条件为真(即文件不存在),即使它在那里

127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) init rewrite engine with requested uri /login
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (3) applying pattern '^(.*)$' to uri '/login'
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (4) RewriteCond: input='/login' pattern='!-f' => matched
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (4) RewriteCond: input='/login' pattern='!-d' => matched
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) rewrite '/login' -> '/index.php//login'
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) local path result: /index.php//login
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) prefixed with document_root to /var/www/html/ci_doctrine/index.php/login
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (1) go-ahead with /var/www/html/ci_doctrine/index.php/login [OK]
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) init rewrite engine with requested uri /login
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (3) applying pattern '^(.*)$' to uri '/login'
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (4) RewriteCond: input='/login' pattern='!-f' => matched
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (4) RewriteCond: input='/login' pattern='!-d' => matched
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) rewrite '/login' -> '/index.php//login'
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) local path result: /index.php//login
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) prefixed with document_root to /var/www/html/ci_doctrine/index.php/login
127.0.0.1 - - [03/May/2010:23:26:56 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (1) go-ahead with /var/www/html/ci_doctrine/index.php/login [OK]
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) init rewrite engine with requested uri /css/style.css
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (3) applying pattern '^(.*)$' to uri '/css/style.css'
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (4) RewriteCond: input='/css/style.css' pattern='!-f' => matched
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (4) RewriteCond: input='/css/style.css' pattern='!-d' => matched
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) rewrite '/css/style.css' -> '/index.php//css/style.css'
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) local path result: /index.php//css/style.css
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (2) prefixed with document_root to /var/www/html/ci_doctrine/index.php/css/style.css
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#167e8e0/initial] (1) go-ahead with /var/www/html/ci_doctrine/index.php/css/style.css [OK]
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) init rewrite engine with requested uri /css/style.css
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (3) applying pattern '^(.*)$' to uri '/css/style.css'
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (4) RewriteCond: input='/css/style.css' pattern='!-f' => matched
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (4) RewriteCond: input='/css/style.css' pattern='!-d' => matched
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) rewrite '/css/style.css' -> '/index.php//css/style.css'
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) local path result: /index.php//css/style.css
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (2) prefixed with document_root to /var/www/html/ci_doctrine/index.php/css/style.css
127.0.0.1 - - [03/May/2010:23:26:58 --0700] [ci_doctrine/sid#13c1868][rid#16848f8/subreq] (1) go-ahead with /var/www/html/ci_doctrine/index.php/css/style.css [OK]

在我开始搞乱重写规则之前,文件... / css / style.css工作正常,所以它应该在正确的位置。但是现在路径总是通过重写来提升,尽管index.php下面的虚拟组件已被正确翻译。我做错了什么?

3 个答案:

答案 0 :(得分:3)

为了实现这一点,我不得不改变ReWriteCond语句,如下所示:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d

REQUEST_FILENAME的文档说“匹配请求的文件或脚本的完整本地文件系统路径”,但对我来说,它始终缺少路径的DocumentRoot部分,这意味着'文件存在'测试永远都是假的。

也许这是一个apache错误,或者只是文档错误。我不知道那部分。 http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

答案 1 :(得分:0)

你已经注释掉了这些规则:

#               RewriteCond ^/css/style.css$  (these have bad syntax, but that's beside the point)
#               RewriteRule ^/css/style.css$ /css/style.css [L]

这些规则中的后一个检测到样式表的请求,将其重写为相同的地址(即什么都不做),然后声明“我是最后一条规则” - 这意味着匹配时,不会运行其他规则,阻止它匹配后来发现的规则。

如果你从这一行的开头拿掉#,你的style.css文件会再次运行,虽然这不是执行地址转换的好方法,因为你必须为每个文件添加一个类似的规则不想改写。

答案 2 :(得分:0)

取消注释style.css的RewriteRule,因为你的RewriteCond是错误的(它应该是RewriteCond %{REQUEST_URI} ^/css/style.css$)并且无论如何都是多余的。