Matt Facer
Matt Facer

Reputation: 3105

htaccess redirect part of filename

I have read several pages on redirecting files and urls with regex in htaccess, but nothing for what I am trying to achieve.

We have old file names such as /images/imagename-70-70.jpg - these have since been replaced by 100x100 versions, so the file name is now /images/imagename-100-100.jpg

We are getting a lot of errors in our log file for requests for the old filename. Is there any way to redirect based on splitting the file name up?

eg:

/images/$1-70-70.jpg should redirect to /images/$1-100-100.jpg

thanks for any help.

Upvotes: 1

Views: 446

Answers (2)

anubhava
anubhava

Reputation: 786091

Keep this as your very first rule:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+images/(.+?)-70-70\.(\S+) [NC]
RewriteRule ^ /photos/%1-100-100.%2 [L,R=301]

Upvotes: 0

dtyler
dtyler

Reputation: 1428

This should work:

RewriteEngine on

RewriteRule ^images/(\w+)-70-70\.jpg$ images/$1-100-100.jpg [L,R=301]  

Upvotes: 1

Related Questions