SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast otherground's Avatar
    Join Date
    Dec 2001
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite: rewrite URLs using sub-string of file names for directory paths?

    I'm trying to use Apache mod_alias / mod_rewrite / scriptaliasmatch to solve the following problem:

    I need requests for files in this format:

    //mysite.com/images/filename.jpg

    to be mapped to files that exist at this location:

    //mysite.com/images/f/i/filename.jpg

    The first two letters of the file name need to be added to the directory path of the file.

    I need this to work for all requests to the images folder, so these URL's

    //mysite.com/images/dog.jpg
    //mysite.com/images/cat.jpg
    //mysite.com/images/bird.jpg

    Need to be mapped to the files:

    //mysite.com/images/d/o/dog.jpg
    //mysite.com/images/c/a/cat.jpg
    //mysite.com/images/b/i/bird.jpg

    What is the best way to accomplish this?

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,268
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    You can capture parts of the original string with (parentheses), then you can use those captured values in the replacement with $1, $2, etc. Here's an example that will probably work. I haven't tested it.

    RedirectMatch 301 ^/images/(([^/])([^/])[^/]*)$ /images/$2/$3/$1
    "First make it work. Then make it better."

  3. #3
    SitePoint Enthusiast otherground's Avatar
    Join Date
    Dec 2001
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much Jeff .. This worked perfectly once I removed the first forward slash!


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
  •