I need to rename many jpg files. For expamle:

warszawa_1.jpg  ->  wars_1.jpg, warszawa_2.jpg  ->  wars_2.jpg

I created in the .htaccess file something like that:

Redirect 301 /img/warszawa_1.jpg        /img/wars_1.jpg

but I would have to enter over 100 like that. Is there any way to get it done with one entry in htaccess?

  • So, you are removing the zawa part of the original request URL? Are you linking to the wars_N.jpg in your code? – MrWhite Feb 10 '20 at 22:15

I'm assuming:

  1. You have already renamed these files in your application.
  2. You are linking to the new filenames in your pages (otherwise you should not implement this as a redirect, but as an internal rewrite instead)
  3. You have no other directives in your .htaccess file. (The order of directives is important and if you are already using mod_rewrite then you should probably use mod_rewrite instead.)
  4. All these requests for images are for the /img subdirectory.

So, you are removing the zawa part of the filename and there are a series of digits after an underscore that need to be preserved.

Try the following:

RedirectMatch 302 ^(/img/wars)zawa(_\d+\.jpg)$ $1$2

Test with 302 (temporary) redirects before changing to a 301 later if required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.