SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mod rewrite with the file extensions

    Hi,

    I have pictures kept in the folder like this www.mywebsite.com/img_content/extra/

    there are list of pictures like,

    1_20100321104422.jpg
    hp_20100321104555.jpg
    ep_m_2010032113455.gif

    so I want to rewrite the url from, for instance,
    http://www.mywebsite.com/img_content...0321104555.jpg
    to
    http://www.mywebsite.com/wallpapers/...0321104555.jpg

    which mean you only see this on your browser url,
    http://www.mywebsite.com/wallpapers/...0321104555.jpg

    this is my code in .Htaccess file but it won't work,
    Code:
    RewriteRule ^wallpapers/([a_zA_Z0_9]+)\.(jpg|gif)$  /img_content/extra/$1.$2 [L]
    Many thanks if you have any ideas.

    Cheers,
    Lau

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I believe your problem lies in your filename regular expression:
    Code:
    ([a_zA_Z0_9]+)
    The underscores are incorrect.

    You need to allow all letters, all numbers and underscores. Fortunately there is a shorthand for [A-Za-z0-9_] which is \w:

    Code:
    RewriteRule ^wallpapers/(\w+)\.(jpg|gif)$   /img_content/extra/$1.$2 [L]
    If that shorthand doesn't work in APACHE (it normally works in PHP and Perl), try the following:
    Code:
    RewriteRule ^wallpapers/([A-Za-z0-9_]+)\.(jpg|gif)$   /img_content/extra/$1.$2 [L]
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    I believe your problem lies in your filename regular expression:
    Code:
    ([a_zA_Z0_9]+)
    The underscores are incorrect.

    You need to allow all letters, all numbers and underscores. Fortunately there is a shorthand for [A-Za-z0-9_] which is \w:

    Code:
    RewriteRule ^wallpapers/(\w+)\.(jpg|gif)$   /img_content/extra/$1.$2 [L]
    Hello! thanks for the reply.

    I have just tried this code with \w but it still won't work...

    I found another short hand for that which is .+ and it works fine...
    Code:
    RewriteRule ^wallpapers/(.+)\.(jpg|gif)$ /img_content/extra/$1.$2 [L]
    thanks mate!

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

    That's not "short hand" but a metacharacter which stand for ANY character. With the + metacharacter as a modifier, it means one or more of anything - which can be quite dangerous. IMHO, use the character range definition Jake offered.

    Regards,

    DK
    Last edited by dklynn; Apr 13, 2010 at 19:39.
    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

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    lauth,

    That's not "short hand" but a metacharacter which stand for ANY character. With the + metacharacter as a modifier, it means one or more of anything - which can be quite dangerous. IMHO, use the character range definition Jake offered.

    Regards,

    DK
    oh got it! thank you for pointing this out
    Last edited by dklynn; Apr 13, 2010 at 19:39.

  6. #6
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,276
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    oops, in dk's post = means +

    + is the metacharacter... one I tend to always mistype as = : (

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,650
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Whoops! Thanks!

    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
  •