labu77
labu77

Reputation: 797

Redirect many images via 301 htaccess / php?

An image gallery has 120,000 images. I want to change URL such as this:

/album_mod/upload/8c3d534a1451c87aca1fa710be6be8.jpg

to this, using a 301 redirect with a .htaccess file:

/image/gallery/category-a8/imagename-i6282.jpg

However, my problem is that the original URL has no ID in the URL. My idea is to:

  1. Make a redirect for all JPG files to /redirect/
  2. Create a index.php in /redirect/
  3. Cut of with php the filename for example "8c3d534a1451c87aca1fa710be6be8"
  4. Select in the DB for "8c3d534a1451c87aca1fa710be6be8".
  5. Take the category and the image name.
  6. Build the new string like:
/image/gallery/category-a8/imagename-i6282.jpg

In this way I have two 301 redirects. Is there a better way?

Upvotes: 0

Views: 748

Answers (1)

Croises
Croises

Reputation: 18671

You can do invisible rewrite to .php with this .htaccess:

RewriteEngine on 
RewriteRule ^album_mod/upload/(.+)\.jpg$ /redirect/index.php?file=$1 [L]

And after that you can do only one 301 redirect, with the right new name.

Upvotes: 2

Related Questions