【问题标题】:.htaccess RewriteCond tested after the rewrite.htaccess RewriteCond 重写后测试
【发布时间】:2011-06-05 11:41:12
【问题描述】:

在过去的两个小时里,我一直在尝试让某些东西正常工作,但没有任何运气。我已经在 google 和 apache docs 上寻求帮助,但我仍然找不到解决这个问题的方法。事情是这样的:

我有两个文件:

index.php
public/css/screen.css

如果不是文件,我希望在浏览器中键入的任何 URL 都重定向到 index.php?url=$1。如果是,它应该重定向到 public/$1。我尝试使用以下重写规则:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) public/$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [QSA,L]

不幸的是,由于重写只发生在满足 RewriteCond 之后,前两个 RewriteCond 将针对 css/screen.css 而不是 public/css/screen.css 进行测试。

是否有一个魔术标志告诉 apache 执行 RewriteRule,测试 RewriteConds 是否为真,如果不是则撤消重写?你们会怎么做呢?

谢谢

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    首先比较 RewriteRule,但除非所有条件都匹配,否则不会发生替换——你不能直接影响它。

    由于 .htaccess 中的 REQUEST_FILENAME 是服务器已经将 URL 映射到的对象,因此您应该这样做,例如:

    RewriteCond %{DOCUMENT_ROOT}/public/$1 -f RewriteRule (.*) public/$1 [L]

    其中 $1 是最初请求的“剩余”部分

    您还需要一个适当的 RewriteBase,因为您正在重写到一个相对 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 2013-09-14
      • 2023-01-25
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多