Using apache to check Accept header

554 views
Skip to first unread message

Rich Bradshaw

unread,
Sep 30, 2010, 4:45:45 PM9/30/10
to WebP Discussion
Assumedly the best way to use webp will be to set apache to check the
Accept header for the image/webp mime type and then serve up a jpg if
that isn't there, or the webp version if it is.

How do you set that up?

Gaz Davidson

unread,
Oct 1, 2010, 9:35:54 AM10/1/10
to WebP Discussion
I guess the easiest way would be to use a rewrite rule which says "If
the accept header contains webp then add .webp to the file name and
change the mime type to image/x-webp" (or whatever the correct
mimetype is)

Here's what I guess it would look like, no promises it will work:

RewriteEngine on
RewriteCond %{HTTP_ACCEPT} .*webp.*
RewriteRule ^(.+\.jpg)$ $1.webp [T=image/x-webp]

(That's assuming that for each foo.jpg there's also a foo.jpg.webp in
the same directory, otherwise you'll get 404s)

Iszak

unread,
Oct 1, 2010, 10:19:48 AM10/1/10
to WebP Discussion
This is what I created, it is a bit more accurate than the previous
rule as it detects for the exact mime type assumed image/webp however
It could be image/x-webp it also ensures that the file exists. I have
tested it and it works in my current environment I hope it helps as a
starting point.

<IfModule mod_rewrite.c>
# Enable rewrite
RewriteEngine On

# Does browser support WebP?
RewriteCond %{HTTP_ACCEPT} \bimage/webp\b

# Capture image name
RewriteCond %{REQUEST_URI} (.*)\.jpe?g$

# Does WebP image exist?
RewriteCond %{DOCUMENT_ROOT}%1.webp -f

# Route to WebP image
RewriteRule .* %1\.webp [L]
</IfModule>

If anyone has any improvements or suggestions please let me know.

On Oct 1, 4:45 am, Rich Bradshaw <rich.brads...@gmail.com> wrote:
> Assumedly the best way to use webp will be to set apache to check the
> Accept header for the image/webpmimetype and then serve up a jpg if

Iszak

unread,
Oct 1, 2010, 10:22:22 AM10/1/10
to WebP Discussion
I just noticed I forgot to include sending the correct mime type, here
is the updated version alternatively you should be able to also do
AddType image/webp .webp

<IfModule mod_rewrite.c>
# Enable rewrite
RewriteEngine On
# Does browser support WebP?
RewriteCond %{HTTP_ACCEPT} \bimage/webp\b
# Capture image name
RewriteCond %{REQUEST_URI} (.*)\.jpe?g$
# Does WebP image exist?
RewriteCond %{DOCUMENT_ROOT}%1.webp -f
# Route to WebP image
RewriteRule .* %1\.webp [L,T=image/webp]
</IfModule>

On Oct 1, 10:19 pm, Iszak <iszak.br...@gmail.com> wrote:
> This is what I created, it is a bit more accurate than the previous
> rule as it detects for the exactmimetype assumed image/webp however

Witold Baryluk

unread,
Oct 1, 2010, 1:46:12 PM10/1/10
to WebP Discussion


On 1 Paź, 16:22, Iszak <iszak.br...@gmail.com> wrote:
> I just noticed I forgot to include sending the correct mime type, here
> is the updated version alternatively you should be able to also do
> AddType image/webp .webp
>
> <IfModule mod_rewrite.c>
>   # Enable rewrite
>   RewriteEngine On
>   # Does browser support WebP?
>   RewriteCond %{HTTP_ACCEPT} \bimage/webp\b
>   # Capture image name
>   RewriteCond %{REQUEST_URI}  (.*)\.jpe?g$
>   # Does WebP image exist?
>   RewriteCond %{DOCUMENT_ROOT}%1.webp -f
>   # Route to WebP image
>   RewriteRule .* %1\.webp [L,T=image/webp]
> </IfModule>
>
> On Oct 1, 10:19 pm, Iszak <iszak.br...@gmail.com> wrote:

One should note, that "-f" will involve disk seek (yes, it is highly
probably it will be in cache),
which can sevarly hurt performance. So if you know for sure that all
your jpeg files have
webp version, then just comment it out (or restrict this rewrite rule
to given directory if you want).

Any way good idea.

Rich Bradshaw

unread,
Oct 2, 2010, 4:03:20 AM10/2/10
to WebP Discussion
Awesome - this should make this using WebP much easier!
Reply all
Reply to author
Forward
0 new messages