2

このようなファイルディレクトリを持つWebサイトがあります

/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/200x150またはhttp://example.com/images/200x180. 2 つの URL から、最初の画像は存在するが 2 番目の画像は存在しないことがわかります/storage/200/200x150.png

だから、私はこれについて書きたいと思い.htaccessます(理論的にはここに)。

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

これどうやってするの?

4

2 に答える 2

1

あなたの例から、画像リクエストの典型的なURLは次のようになります

http://example.com/images/WidthxHeight

ここでWidth、およびHeightは変数でimagesあり、固定文字列です。

また、一般的な置換URLは次のようになります。

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

ここWidthで、およびHeightは着信URLから渡されるパラメーターであり、storageおよびpngは固定文字列です。

あなたはこれを試すことができます:

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]

アップデート

パラメータが1つあり、パラメータがない着信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]に置き換えます

于 2013-03-16T21:44:07.970 に答える
0

.htaccess の現在の設定方法によっては、存在しないすべてのファイルを failed.php にルーティングできます。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /failed.php [L]
于 2013-03-16T17:30:42.323 に答える