【问题标题】:.htaccess redirect to file if file exists如果文件存在,.htaccess 重定向到文件
【发布时间】:2012-12-02 18:10:55
【问题描述】:

我想通过使用 htaccess 执行以下操作:

在地址栏中:http://images.domain.ext/100x100/1234/some_text.jpg 服务器上:http://images.domain.ext/100x100/1234.jpg

我已经用过:

RewriteRule ^100x100/(.*)/(.*)\.jpg$ /100x100/$1.jpg

这可行,但如果文件不存在,它也会重定向。 仅当文件存在于服务器上时,我才需要重定向。 如果文件不存在,我想去index.php

我认为这与 RewriteCond -f 有关,但我不知道如何更改 RewriteCond 中的文件名。

RewriteCond /100x100/$1.jpg -f
RewriteRule ^100x100/(.*)/(.*)\.jpg$ /100x100/$1.jpg

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    你需要在Regex Back-Reference Availability中引入你的Document Root如图1所示:

    RewriteCond %{DOCUMENT_ROOT}/100x100/$1.jpg -f
    RewriteRule ^100x100/(.*)/(.*)\.jpg$ /100x100/$1.jpg
    

    【讨论】:

      猜你喜欢
      • 2011-09-12
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 2019-11-23
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多