Phil Cross
Phil Cross

Reputation: 9302

mod_rewrite remapping folder

I've currently updated our site, and the image folder name has changed from /img/ to /images/.

I'm still getting 404 errors in my apache error log from bots etc trying to access the old /img folder.

I'm trying to write a mod_rewrite rule to redirect any attempts to access /img/ to refer to /images/.

This is what I've got so far:

RewriteRule ^img/?(.*)$ images/$1 [R=301,L]

However, whenever I access http://mysite.com/img I still get my 404 page (instead of a forbidden page which I should receive for accessing /images).

Is this correct? I do have another rule forcing use of ssl if that matters.

Many thanks

Upvotes: 2

Views: 1042

Answers (1)

Prix
Prix

Reputation: 19528

This rule should be place on your root folder:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/images/$1 -d [OR]
RewriteCond %{DOCUMENT_ROOT}/images/$1 -f
RewriteRule ^img/(.*) /images/$1 [R=302,NC,L]

This rule will redirect only existent files or folders existent on http://domain.com/images.

Keep in mind that you may have been cached from previous attempts since you're using R=301, so to make sure its working try using a different browser.

Note that I am using R=302, to avoid this caching, once you confirm it is working, change it to R=301.

Upvotes: 3

Related Questions