Chris Nicholson
Chris Nicholson

Reputation: 11

Redirect images folder using htaccess

I'm trying to redirect my images folder (specifically jpg files) to a different location when a mobile/tablet is used. This is what I've currently got, but it keeps bringing up a 500 Internal Server Error when I view the images.

RewriteCond %{HTTP_USER_AGENT} iphone|ipad|ipod|android|blackberry [NC]
RewriteRule ^images/(.+)\.(jpg)$ http://www.domain.co.uk/images/mobile/$1.$2 [QSA,L]

Thanks

Upvotes: 0

Views: 117

Answers (1)

Midhun Babu
Midhun Babu

Reputation: 129

You're getting the error because of looping. Try the below code:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC] 
RewriteCond %{REQUEST_URI} !/images/ [NC]
RewriteRule \.(png|gif|jpe?g)$ images%{REQUEST_URI} [L,NC]

This will rewrite image URLs only when /images/ is not present in URI.

Upvotes: 1

Related Questions