Grainier
Grainier

Reputation: 1654

Redirect a .jpg using .htaccess

I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"

Therefore I've used

RewriteRule (^|.*?/)image/(.*)$ /$1view/$2 [R=302,L,NC]

But, still if I try to visit "example.com/image/img.jpg" it won't redirect. What am I doing wrong?

Thank you.

Upvotes: 1

Views: 3350

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"

Apparently the above examples are not accurate as your rule shows directories image and view can be at any level in the corresponding URL directory structures.

If that's the case, you may try this in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)?image/([^.]+)\.jpg [NC]
RewriteRule .*             /%1view/%2.jpg        [R,L,NC]

In case image and view directories are indeed at the first level as described in the examples, replace the last 2 lines with this one:

RewriteRule ^image/(.*)   /view/$1 [R,L,NC]

Replace [R,L,NC] with [R=301,L,NC] for permanent redirection or with [L,NC] for internal mapping.

Upvotes: 1

Related Questions