【问题标题】:Apache .htaccess image exists check and redirectApache .htaccess 图像存在检查和重定向
【发布时间】:2013-03-16 17:12:26
【问题描述】:

我有一个像这样的文件目录的网站

/index.php (home page)

/storage (file storage directory)
    /800 (800 pixel width dir)
        /800x200.png
        /800x350.png
        ....
    /200 (200 pixel width dir)
        /200x150.png
        /200x185.png
        ...
    ....

/css
    /style.css

/images
    /logo.png

/jscript
    /autoload.js

现在用户将发出请求http://example.com/images/200x150http://example.com/images/200x180。从两个 URL 我们知道第一张图片存在于/storage/200/200x150.png 但不存在第二张。

所以,我想为此写.htaccess(理论上是这里)。

Rewrite Condition /storage/{width}/{widthxheight}.png existed?
Rewrite Rule {output the image}
Rewrite Failed {go to /somedir/failed.php}

我该怎么做?

【问题讨论】:

  • RewriteCond path/to/file -f 这是文件存在的简单规则...
  • 我确实建议 @Marshal 解释为不存在的文件创建不依赖于存在的规则...

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

从您的示例中,图像请求的典型 URL 如下所示

http://example.com/images/WidthxHeight

其中WidthHeight 是变量,images 是固定字符串。

典型的替换 URL 应该是这样的:

http://example.com/storage/Width/WidthxHeight.png

其中WidthHeight是传入URL传递的参数,而storagepng是固定字符串。

你可以试试这个:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# Make sure the request is for an image file
RewriteCond %{REQUEST_URI}  ^/images/([^x]+)x([^/]+)/?   [NC]

# Don't want loops
RewriteCond %{REQUEST_URI}  !storage                     [NC]

# Make sure the file exists
RewriteCond %{REQUEST_FILENAME}    -f

# If all conditions are met, rewrite
RewriteRule .*   /storage/%1/%1x%2.png                   [L]

## Else, map to failed.php
RewriteCond %{REQUEST_URI}  !storage                     [NC]
RewriteCond %{REQUEST_URI}  !failed\.php                 [NC]
RewriteRule .*   /somedir/failed.php                     [L]

更新

对于传入 URL 有 2 个附加规则,一个参数和一个参数。

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

## New option 1
## Check if the request has any parameter
RewriteCond %{REQUEST_URI}  !storage                   [NC]
RewriteCond %{REQUEST_FILENAME}    -f
RewriteRule ^images/?$   /storage/200/200x200.png      [L,NC]

## New option 2
## Check if the request has only 1 parameter
RewriteCond %{REQUEST_URI}  !x                          [NC]
RewriteCond %{REQUEST_URI}  !storage                    [NC]
RewriteCond %{REQUEST_FILENAME}    -f
RewriteRule ^images/([^/]+)/?$   /storage/$1/$1x$1.png  [L,NC]

## Check if the request has 2 parameters
RewriteCond %{REQUEST_URI}  !storage                    [NC]
RewriteCond %{REQUEST_FILENAME}    -f
RewriteRule ^images/([^x]+)x([^/]+)/?  /storage/$1/$1x$2.png  [L,NC]

## Else, map to failed.php
RewriteCond %{REQUEST_URI}  !storage                     [NC]
RewriteCond %{REQUEST_URI}  !failed\.php                 [NC]
RewriteRule .*   /somedir/failed.php                     [L]

对于永久且可见的重定向,请将 [L,NC] 替换为 [R=301,L,NC]

【讨论】:

  • @CodeProtocol 不,这不起作用,因为 OR 只能在条件下使用。如果我理解正确,当传入 URL 中只有一个参数时,它必须同时用于高度和宽度,对吗?如果是这样,您将需要另一个规则,我可以使用该选项更新我的答案。请确认。
  • 是的,如果只有images,那么storage/200/200x200.png。如果images/250 然后storage/250/250x250.png 如果images/250x120 然后storage/250/250x120.png
  • 当传入的URL没有参数时,是不是一直是200
  • 问题是条件只适用于下一个规则,并且每个规则都需要。另一方面,参数不能是可选的,因为替换 URL 在每种情况下都不同,所以据我所知,没有办法缩短代码。
  • 更新为在整个规则集中保存 3 行的版本。这是我能做的最好的了。
【解决方案2】:

根据您的 .htaccess 当前设置方式,您可以将所有不存在的文件路由到 failed.php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /failed.php [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2023-03-15
    相关资源
    最近更新 更多