Rewrite images to images directory

Discussion in 'Apache' started by dieselcatalog, Sep 5, 2010.

  1. #1
    The lady that used to own the site I'm running put all of her images into the root directory. Literally thousands of images. The site is huge, and we get tons of traffic from those images so I don't want to just move them without rewriting them to an images folder.

    Here is what I have already:
    #RewriteCond %{REQUEST_URI} !/images/.*
    #RewriteRule ^(.*\.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG))$ /images/$1 [QSA,L]
    Code (markup):
    The problem is that I believe the above code was also grabbing images from all directories. I would like the rewrite rule to some how apply ONLY to the root directory, and leave all of the images outside of the root directory alone.

    Is that possible?

    Thanks!
     
    dieselcatalog, Sep 5, 2010 IP
  2. dieselcatalog

    dieselcatalog Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is it possible to do this and make it relevant to the root directory only? Thanks for your help
     
    dieselcatalog, Sep 6, 2010 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    Assuming the server is running at least Apache 2.2 where Perl 5 syntax is available, you can try excluding anything that has a directory separator in it,

    RewriteRule ^([^/]+)\.(jpe?g|gif|png)$ /images/$1.$2 [L,NC]
    Code (markup):
    You don't need the QSA flag because you're not altering the querystring in your replacement so QSA is automatically implied.
    If you use the NC (no case) flag you don't have to list the lower and upper case variants of the file extensions.

    You may want to go through your other directories and explicitly turn RewriteEngine off in them.
     
    joebert, Sep 15, 2010 IP