あなたの例から、画像リクエストの典型的な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]に置き換えます