Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am working on a site migration and I need to set up some redirects for image files. Certain image path requests will come in to root-level subdirectories and I need to put /new_directory/ as the first directory for all of them, like so:

Old site request -> new request
website.com/dir1/image.png -> website.com/new_directory/dir2/subdir1/image.gif
website.com/dir2/subdir1/image.gif -> website.com/new_directory/dir2/subdir1/image.gif

Here's what I have so far, but it doesn't appear to be working. What am I missing?

RewriteCond %{HTTP_REQUESTURI} (dir1|dir2|dir3)(.+?)(jpg|png|gif|jpeg)    [NC,OR]
RewriteCond %{HTTP_REQUESTURI} (dir4|dir5|dir6)(.+?)(jpg|png|gif|jpeg)    [NC,OR]
RewriteCond %{HTTP_REQUESTURI} (dir7|dir8|dir9)(.+?)(jpg|png|gif|jpeg)     [NC]
RewriteRule ^(.+?)$  /new_directory/$1 [R=301,L]

I have several RewriteCond lines broken up because my actual list of directories is quite long, and I want the rules to fit on one screen width.

share|improve this question
    
Was there a copy-paste error in the first sample redirection, or is that actually the behavior you want? –  Andrew M. Oct 12 '11 at 2:49

2 Answers 2

up vote 0 down vote accepted

This works for me; make sure that you have RewriteEngine on, otherwise redirects won't be processed. If you want to see whether or not mod_rewrite is being hit at all, use the RewriteLog:

RewriteLog /tmp/rewrite.log
RewriteLogLevel 3

If you receive NO output whatsoever after a restart, mod_rewrite hasn't even been enabled.

Additionally, you can simplify your code like so:

RewriteCond %{REQUEST_URI} ^/dir[1-9]/.+\./(jpg|png|gif|jpeg)$ [NC]
RewriteRule ^/(.*)$ /new_directory/$1 [R=301,L]

This will do the same as yours, except:

  • It will no longer match /some/other/dir1/image.gif, /dir1/jagif, or /dir1isagif.
  • Redirected URLs will no longer contain double slashes: /new_directory//dir1/image.gif.
  • You no longer need to worry about greedy or non-greedy searches.

Let me know if I misunderstood your question!

share|improve this answer

Go there to test your regexp http://www.quanetic.com/Regex (of course check "ereg" box)

Then here's your optimized (and tested through the previous site) RewriteRule:

RewriteCond %{HTTP_REQUESTURI} (dir(1|2|3|4|5|6|7|8|9))(.*)\.(jpg|png|gif|jpeg)$
RewriteRule (.*)  /new_directory%3\.%4 [R=301,L]

For you information, %3 and %4 are what is extracted from RewriteCond (= you ignore the (.*) in the RewriteRule). Example: with URI "/subrep1/subrep2/mypic.jpg":

Substrings extracted:

%1 = dir1
%2 = 1
%3 = /subrep1/subrep2/mypic
%4 = jpg

Then in the RewriteRule it will be transformed to:

RewriteRule (.*)  /new_directory%3\.%4 [R=301,L]

Thus

/new_directory/subrep1/subrep2/mypic.jpg
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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