Malachi
Malachi

Reputation: 977

.htaccess redirect for images from old folder to new dynamically created folder structure

I saw the following answer on a similar question:

//301 Redirect Entire Directory
RedirectMatch 301 /blog/wp-content/uploads/(.*) /wp-content/uploads/$1

Which is great, but I have an additional question on this: Say I have the following structure:

examples:

/blog/photos/default/1234/.photos/filename.jpg
/blog/photos/default/12/.photos/filename2.jpg
/blog/photos/default/123456/.photos/filename3.jpg

and they now need to be redirect to:

/blog/photos/default/id/3/4/1234/.photos/filename.jpg
/blog/photos/default/id/1/2/12/.photos/filename2.jpg
/blog/photos/default/id/5/6/123456/.photos/filename3.jpg

As you can see, ther is now an additional structure there: /id/x/y where x is the 2nd to last digit of the original id and y is the last digit.

How can this be done? (there will not be a case where there is less than 2 digits, but the old id can be like 25 digits long..)

Upvotes: 2

Views: 66

Answers (1)

anubhava
anubhava

Reputation: 785991

You can use this rule for that:

RedirectMatch 301 ^(/blog/photos/default)/(\d*(\d)(\d)/.+)$ $1/id/$3/$4/$2

Upvotes: 1

Related Questions