pandronic
pandronic

Reputation: 651

Apache mod_rewrite and regular expression

In my HTML I want to refer to my images like this:

/images/articles/this-is-the-page-title-dgj4klci5j4.jpg
/images/articles/thumbs/this-is-the-page-title-dgj4klci5j4.jpg
/images/gallery/this-is-the-page-title-qj3k56l6kfm.jpg

but they are really located at:

/_pics/articles/dgj4klci5j4.jpg
/_pics/articles/thumbs/dgj4klci5j4.jpg
/_pics/gallery/qj3k56l6kfm.jpg

My .htaccess file is currently in the root directory of the site and looks something like this:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [R=permanent,L]

RewriteRule ^articles/ user/articles.php [L]
RewriteRule ^gallery/ info/gallery.php [L]

I have to do this because in the last possible moment the client decided that he wants more legible image filenames and this seems like a quick reasonable fix.

I imagine that I could redirect everything from /images to /_pics and take the page title out by just using mod_rewrite. Problem is that I'm not very good with regular expressions and I don't fully understand the way mod_rewrite works. Can I get some help? Thanks

The real image filename only contains letters and numbers and can be jpg or png.

Upvotes: 0

Views: 157

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

Try adding this rule in the htaccess file:

RewriteRule ^images/(.+)/[^/]+-([^/-]+)\.(jpe?g|gif|png)$ /_pics/$1/$2.$3 [L]

Upvotes: 2

Related Questions