【问题标题】:apache mod_rewrite rule in httpd.conf for modifying some paths, but not othershttpd.conf 中的 apache mod_rewrite 规则,用于修改某些路径,但不修改其他路径
【发布时间】:2010-05-04 06:34:12
【问题描述】:

我在为 Fedora 10 上的 Apache/2.2.14 创建适当的重写规则时遇到了很大的挑战。我正在处理使用 .htaccess 文件的CodeIgniter-Doctrine tutorial。 (搜索 Removing “index.php” from CodeIgniter urls 大约降低了 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 下面的虚拟组件被正确翻译了。我做错了什么?

【问题讨论】:

    标签: codeigniter apache-config


    【解决方案1】:

    为了让它工作,我必须像这样更改 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

    【讨论】:

      【解决方案2】:

      您已注释掉这些规则:

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

      这些规则中的后者检测到对样式表的请求,将其重写到相同的地址(即什么都不做),然后声明“我是最后一个规则”——这意味着当匹配时,将不再运行任何规则,这阻止它匹配后来找到的规则。

      如果你去掉这一行开头的 #,你的 style.css 文件将再次工作,尽管这不是执行地址转换的好方法,因为你必须为你的每个文件添加一个类似的规则不想重写。

      【讨论】:

      • 我将这些注释掉了,因为它们阻止了 apache 重新启动。错误消息是:Syntax error on line 1022 of /etc/httpd/conf/httpd.conf: RewriteCond: bad argument line '^/css/style.css$'
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-09
        • 2015-04-24
        • 2011-03-25
        • 1970-01-01
        • 1970-01-01
        • 2015-09-17
        • 1970-01-01
        • 2014-01-07
        相关资源
        最近更新 更多