Question

I have a very large site which has lots of images which have a url like /dir1/dir2/images/xyz.jpg or /dir1/images/xyz.jpg`. (any number of directories before leading up to the page).

These images have to be moved to a single images directory at the root of the site and rather than going in and altering all the urls is it possible using the .htaccess file to point them to /images.xyz.jpg? (with the slash at the beginning)

I already have this code at the beginning of my htaccess file for other url cleaning but can't make this image url altering work.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(downloads|includes)($|/) - [L]
Était-ce utile?

La solution

You can do:

RewriteEngine On

RewriteRule ^(.*?)(images)/(.+)$ /$1$2.$3 [L,NC,R=302]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(downloads|includes)($|/) - [L]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top