【问题标题】:apache2 mod_rewrite per-directory request in rewritecondrewritecond 中的 apache2 mod_rewrite 每个目录请求
【发布时间】:2015-12-25 02:04:56
【问题描述】:

我当前的 .htaccess 文件可以工作,但我认为这不是解决这个问题的最佳方法,而且我担心它在未来有更多的重写规则时不会工作。

.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^files/(.*) files/$1 [END]

RewriteRule ^(.*) files/$1 [END]
</IfModule>

服务器检查 url 是否指向“files”文件夹,只有在指向其他地方时才会重写。

我尝试了类似的东西

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule !^files/(.*) files/$1 [END]
</IfModule>

但它不起作用,因为我无法传递 $1 变量。我不想要这样的东西

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{URL_STARTING_AT_THIS_POINT} ^files/*
RewriteRule ^(.*) files/$1 [END]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !%{URL_TO_THIS_HTACCESS}/files/*
RewriteRule ^(.*) files/$1 [END]
</IfModule>

我没有为此找到任何服务器变量。我想比较 RewriteCond 中 RewriteRule (^files/) 的第一个参数,但没有找到正确的服务器变量。

【问题讨论】:

  • 我有点困惑。你也想比较第一个参数。你不想发生什么?
  • 例如网址是www.myserver.com/path/to/htaccess/files/index.html。我的.htaccess 位于文件夹 htaccess 中。所以此时RewriteRule的第一个参数是files/index.html。我想知道,如何在 RewriteCond 中使用这个参数(以文件夹 htaccess 开头的 URL)。

标签: apache .htaccess mod-rewrite


【解决方案1】:

我没有找到任何服务器变量。

您可能需要的服务器变量是 REQUEST_URI - 它包含 URL 路径,包括斜杠前缀。例如:

RewriteCond %{REQUEST_URI} !^/files/
RewriteRule (.*) files/$1 [L]

或者,您可以简单地这样写:

RewriteRule !^files/ files%{REQUEST_URI} [L]

但是,您的原始指令可能还可以,尽管可以稍微整理一下:

RewriteRule ^files/(.*) - [L]
RewriteRule (.*) files/$1 [L]

当 URL 没有变化时,可以使用替换中的连字符。我不认为你真的需要 END 标志,除非你有其他可能冲突的指令?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多