问题描述
我使用的是服务器端的图像缩放,自动成为了图像在正确的大小。
图片大小调整需要它的参数从请求URL调整大小,所以我给自己定了一些的.htaccess重写规则,使网址的图像有点prettier:
重写规则^ *照片/ homehero /(.*)$ / V2 / IMGR / w1140-H640-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
使用上面,我可以在我的code使用 http://example.com/photos/homehero/file.jpg
,它返回一个照片,在正确的大小。这工作完全按原样,并且缩放工具后自动他们产生这样的网站是相当迅速的在服务器上的缓存图像。我有5个不同大小的图像的时刻。
考虑到屏幕尺寸和带宽的考虑,我想就可以提供一组独立的图像的宽度/高度降低( w1140-H640
以上)而且质量也降低( Q100
以上)为移动用户。
我添加了一个重写条件来检测用户代理与第二组重写规则。这是低于原来的规则,例如:
的RewriteCond%{HTTP_USER_AGENT}(机器人|黑莓| Googlebot的移动| iemobile | iphone | ipod的| Opera移动| PalmOS的| webOS的)[NC]
重写规则^ *照片/ homehero /(.*)$ / V2 / IMGR / W570-H320-c16x9-Q50-P1 / V2 /照片/ $ 1 [R = 301]
不过这似乎并没有工作。我试图把上述第一,第二块,但没有任何工作,而且我也试着使用未标记的用户代理(%{HTTP_USER_AGENT}(机器人|!
...)正常的重写规则块之前,但没有奏效。
我用Google搜索和搜索计算器,发现类似的,但是,没有问题,所以我想知道如果这实际上可以在.htaccess来实现?我完全规则块以下,任何帮助将是非常美联社preciated。
#Image大小调整标准尺寸
重写规则^ *照片/ homehero /(.*)$ / V2 / IMGR / w1140-H640-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/瓦小/(.*)$ / V2 / IMGR / w504-H252-c6x3-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/英雄/(.*)$ / V2 / IMGR / w1140-H200-c57x10-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/ productshot /(.*)$ / V2 / IMGR / w1140-H640-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/ gallerythumb /(.*)$ / V2 / IMGR / W160-H90-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
#移动网站重定向
的RewriteCond%{HTTP_USER_AGENT}(机器人|黑莓| Googlebot的移动| iemobile | iphone | ipod的| Opera移动| PalmOS的| webOS的)[NC]
重写规则^ *照片/ homehero /(.*)$ / V2 / IMGR / W570-H320-c16x9-Q50-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/瓦小/(.*)$ / V2 / IMGR / w504-H252-c6x3-Q50-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/英雄/(.*)$ / V2 / IMGR / W570-H100-c57x10-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/ productshot /(.*)$ / V2 / IMGR / W570-H320-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/ gallerythumb /(.*)$ / V2 / IMGR / W160-H90-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
您应该把上面的桌面规则移动规则。此外,您还需要重复的RewriteCond每一个重写规则。
于是
#移动网站重定向
的RewriteCond%{HTTP_USER_AGENT}(机器人|黑莓| Googlebot的移动| iemobile | iphone | ipod的| Opera移动| PalmOS的| webOS的)[NC]
重写规则^ *照片/ homehero /(.*)$ / V2 / IMGR / W570-H320-c16x9-Q50-P1 / V2 /照片/ $ 1 [R = 301]
的RewriteCond%{HTTP_USER_AGENT}(机器人|黑莓| Googlebot的移动| iemobile | iphone | ipod的| Opera移动| PalmOS的| webOS的)[NC]
重写规则^ *照片/瓦小/(.*)$ / V2 / IMGR / w504-H252-c6x3-Q50-P1 / V2 /照片/ $ 1 [R = 301]
的RewriteCond%{HTTP_USER_AGENT}(机器人|黑莓| Googlebot的移动| iemobile | iphone | ipod的| Opera移动| PalmOS的| webOS的)[NC]
重写规则^ *照片/英雄/(.*)$ / V2 / IMGR / W570-H100-c57x10-Q100-P1 / V2 /照片/ $ 1 [R = 301]
的RewriteCond%{HTTP_USER_AGENT}(机器人|黑莓| Googlebot的移动| iemobile | iphone | ipod的| Opera移动| PalmOS的| webOS的)[NC]
重写规则^ *照片/ productshot /(.*)$ / V2 / IMGR / W570-H320-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
的RewriteCond%{HTTP_USER_AGENT}(机器人|黑莓| Googlebot的移动| iemobile | iphone | ipod的| Opera移动| PalmOS的| webOS的)[NC]
重写规则^ *照片/ gallerythumb /(.*)$ / V2 / IMGR / W160-H90-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
#Image大小调整标准尺寸
重写规则^ *照片/ homehero /(.*)$ / V2 / IMGR / w1140-H640-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/瓦小/(.*)$ / V2 / IMGR / w504-H252-c6x3-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/英雄/(.*)$ / V2 / IMGR / w1140-H200-c57x10-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/ productshot /(.*)$ / V2 / IMGR / w1140-H640-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
重写规则^ *照片/ gallerythumb /(.*)$ / V2 / IMGR / W160-H90-c16x9-Q100-P1 / V2 /照片/ $ 1 [R = 301]
I am using a server-side image resizer to automatically serve up images at the correct size.
The image resizer takes its parameters for resizing from the request URL, so I've set up some .htaccess rewrite rules to make the URLs for the images a bit prettier:
RewriteRule ^.*photos/homehero/(.*)$ /v2/imgr/w1140-h640-c16x9-q100-p1/v2/photos/$1 [R=301]
Using the above, I can use http://example.com/photos/homehero/file.jpg
in my code and it returns a photo at the correct size. This works perfectly as is, and the resizer automatically caches images on the server after they're generated so the site is reasonably speedy. I have 5 different sized images at the moment.
Taking into account screen-size and bandwidth considerations, I'd like to serve up a separate set of images with the width/height reduced (w1140-h640
above) and the quality reduced too (q100
above) for mobile users.
I've added a rewrite condition to detect user agent with a second set of rewrite rules. This is below the original rules, eg:
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^.*photos/homehero/(.*)$ /v2/imgr/w570-h320-c16x9-q50-p1/v2/photos/$1 [R=301]
However this doesn't seem to work. I have tried putting the second block above the first but that didn't work either, and I've also tried using a not flag for the user agent (%{HTTP_USER_AGENT} "!(android|
...) before the "normal" rewrite rule block, but that didn't work.
I have googled and searched StackOverflow and found similar-but-not questions, so I'm wondering if this can actually be achieved in .htaccess? My full rule block is below, any help would be much appreciated.
#Image Resizer Standard sizes
RewriteRule ^.*photos/homehero/(.*)$ /v2/imgr/w1140-h640-c16x9-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/tile-small/(.*)$ /v2/imgr/w504-h252-c6x3-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/hero/(.*)$ /v2/imgr/w1140-h200-c57x10-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/productshot/(.*)$ /v2/imgr/w1140-h640-c16x9-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/gallerythumb/(.*)$ /v2/imgr/w160-h90-c16x9-q100-p1/v2/photos/$1 [R=301]
# mobile site redirection
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^.*photos/homehero/(.*)$ /v2/imgr/w570-h320-c16x9-q50-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/tile-small/(.*)$ /v2/imgr/w504-h252-c6x3-q50-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/hero/(.*)$ /v2/imgr/w570-h100-c57x10-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/productshot/(.*)$ /v2/imgr/w570-h320-c16x9-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/gallerythumb/(.*)$ /v2/imgr/w160-h90-c16x9-q100-p1/v2/photos/$1 [R=301]
You should put the mobile rules above the desktop rules. Also, you need to repeat the RewriteCond for every RewriteRule.
So
# mobile site redirection
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^.*photos/homehero/(.*)$ /v2/imgr/w570-h320-c16x9-q50-p1/v2/photos/$1 [R=301]
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^.*photos/tile-small/(.*)$ /v2/imgr/w504-h252-c6x3-q50-p1/v2/photos/$1 [R=301]
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^.*photos/hero/(.*)$ /v2/imgr/w570-h100-c57x10-q100-p1/v2/photos/$1 [R=301]
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^.*photos/productshot/(.*)$ /v2/imgr/w570-h320-c16x9-q100-p1/v2/photos/$1 [R=301]
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^.*photos/gallerythumb/(.*)$ /v2/imgr/w160-h90-c16x9-q100-p1/v2/photos/$1 [R=301]
#Image Resizer Standard sizes
RewriteRule ^.*photos/homehero/(.*)$ /v2/imgr/w1140-h640-c16x9-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/tile-small/(.*)$ /v2/imgr/w504-h252-c6x3-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/hero/(.*)$ /v2/imgr/w1140-h200-c57x10-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/productshot/(.*)$ /v2/imgr/w1140-h640-c16x9-q100-p1/v2/photos/$1 [R=301]
RewriteRule ^.*photos/gallerythumb/(.*)$ /v2/imgr/w160-h90-c16x9-q100-p1/v2/photos/$1 [R=301]
这篇关于桌面/移动htaccess的重写规则多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!