【问题标题】:Using altered %{REQUEST_URI} in RewriteCond file exists check在 RewriteCond 文件中使用更改的 %{REQUEST_URI} 存在检查
【发布时间】:2009-05-16 18:36:47
【问题描述】:

我想要获取诸如 http://localhost/universe/networks/o2/o2_logo.gif 之类的 URL 并执行以下操作:

如果它以 /universe/ 开头 从中剥离 /universe/ 成为 /networks/o2/o2_logo.gif 检查这是否存在于 %{DOCUMENT_ROOT}/networks/o2/o2_logo.gif 如果是这样,静默地将请求重写到该文件。

如果我使用以下规则:

RewriteCond %{REQUEST_URI} ^/universe/ 重写规则 ^(.*)$ http://www.example.org/$1 [L]

http://localhost/universe/networks/o2/o2_logo.gif 被重写为 http://www.example.org/networks/o2/o2_logo.gif,这很棒,但我似乎无法使用

我怎样才能继续使用这个“更改”的 %{REQUEST_URI},比如 $1 之类的?

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    我无法完全使用 .htaccess 来解决这个问题,但使用了以下文件:

    RewriteEngine On
    
    # Does the file exist in the content folder?
    RewriteCond %{REQUEST_URI} !^/content.*
    RewriteCond %{DOCUMENT_ROOT}/universe/%{REQUEST_URI} -f
    
    # File Exists
    RewriteRule ^ %{DOCUMENT_ROOT}/universe/%{REQUEST_URI} [L]
    
    # If the request is not a file, link or directory then send to index.php
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L] # Item exists so don't rewrite
    
    # Nothing exists for the request so append a trailing / if needed
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
    
    RewriteRule ^.*$ index.php [NC,L] # Send off as-is to index.php
    

    然后我进入我的 document_root 文件夹并执行以下操作:

    cd /var/www/journal
    ln -s journal/public universe
    cd /var/www/journal/public
    ln -s content/ universe
    

    结合 htaccess 意味着一切正常。

    【讨论】:

    • 这是一个绝妙的答案。我尝试了其他几个人的想法,然后发现了这一点。伙计... htaccess 是一个地狱般的配置文件...
    【解决方案2】:

    试试这个规则:

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

    【讨论】:

    • 看起来它正朝着正确的方向前进。我的 .htaccess 包含以下内容: # 该条目是否存在于内容文件夹中? RewriteCond %{DOCUMENT_ROOT}/universe/public/content/$1 -f RewriteRule ^universe/(.*) localhost:81/exists/$1 [L] RewriteRule ^universe/(.*) localhost:81/not_exists/$1 [L] 当我仍然生成 404 '希望它至少遵循两个 RewriteRules 之一?我会错过什么让它忽略这一点?内容位于 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\universe\public\content
    • 哦,所以你想把它传递给本地网络中的服务器。然后你需要使用代理。只需将 P 标志添加到规则中即可。
    猜你喜欢
    • 2020-02-09
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2014-07-27
    • 2011-03-16
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多