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,
www.mywebsite.com/img_content/extra/hp_20100321104555.jpg
to
www.mywebsite.com/wallpapers/hp_20100321104555.jpg

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

this is my code in .Htaccess file but it won’t work,

RewriteRule ^wallpapers/([a_zA_Z0_9]+)\\.(jpg|gif)$  /img_content/extra/$1.$2 [L]

Many thanks if you have any ideas.

Cheers,
Lau

I believe your problem lies in your filename regular expression:

([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:

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:

RewriteRule ^wallpapers/([A-Za-z0-9_]+)\\.(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…

RewriteRule ^wallpapers/(.+)\\.(jpg|gif)$ /img_content/extra/$1.$2 [L]

thanks mate!
:smiley:

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 :smiley:

oops, in dk’s post = means +

  • is the metacharacter… one I tend to always mistype as = : (

Whoops! Thanks!

Regards,

DK