SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image not found - rewrite img url and try again - in .htaccess

    Hey Guys,

    I've rebuilt a site recently and in the process of tidying up the directories I created a pages directory and moved all html content in there.

    My problem is that for some older parts of the site - the images are a directory further up than the relative url points to

    old src: ../images/this.gif
    new src: ../../images/this.gif

    I wonder if there's a way to rewrite *.gif / *.jpg / *.png files so that if they're not found you look change the url?

    In lay man's terms

    if image not found

    rewrite http://www.mysite.com/pages/images/this.gif
    to http://www.mysite.com/images/this.gif


    (BTW I thought of moving the old images directory into the new pages directory... but unfortunately the old site has stuff all over the place and this isn't garunteed to work...)

    Cheers!

  2. #2
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    An attempt

    I seem to be really bad at this... I've realised that there are no images directories in the pages directory - so all I really need to do is remove the /pages/ from the URL

    This is what I came up with - but it does nothing

    RewriteEngine on
    RewriteRule ^/pages/(.*)/(.*)/(.*)\.jpg$ $1/$2/$3\.jpg$ [L]

    Am I even correct in thinking that .htaccess will rewrite relative paths to images for me?

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    mani,

    First (and foremost), WELCOME to SitePoint and to the Apache Forum!

    Now, it appears that you do not know regex so, because that's such a major part of mod_rewrite, let me recommend that you go through the tutorial Article linked in my signature (note the (.*) is a garbage collector so you are NOT using it properly, i.e., you don't really want to match pages///.jpg, do you?).

    If that doesn't hand the answer to you on a silver platter, please ask again.

    Also, note the Apache 1.x and Apache 2.x handle the {REQUEST_URI} string differently, i.e., if your server is A2, the leading slash will prevent any matching.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •