cninsd
cninsd

Reputation: 162

Apache rewrite remove .jpg.html from URL

I've been trying to write a rewrite rule for apache to switch my gallery2 URLs to the gallery3 URL format:

Old url example linked: http://domain.com/gallery/photoalbumxyz/photo.jpg.html
New url example needed: http://domain.com/photos/photoalbumxyz/photo

Note that in the URL example above, "/photoalbumxyz/photo.jpg.html" is not an actual physical directory, it is just the way gallery2 rewrote "friendly" URLs. I can rewrite the /gallery/ to /photos/ by using a rule like the following:

RewriteRule ^(.*)$ /photos/$1   [QSA,L,R=301]

However I'm having trouble figuring out the matching and removal of the ".jpg.html" extension if it exists in combination with the /gallery/ -> /photos/ rewrite. The regex matching I believe is going to be .jpg.html to escape the periods, but how do I write rules to remove the ".jpg.html" extension and rewrite the directory?

RewriteRule ^\.jpg\.html$ $1 [NC]
RewriteRule ^(.*)$ /photos/$1   [QSA,L,R=301]

Edit: Sorry! I neglected earlier to mention the album URL formats can change (doesn't have to specify a photo, and can include sub albums), I've added some specific examples:

The url rewrite rule also needs to follow:
old: http://example.com/gallery
new: http://example.com/photos

old: http://example.com/gallery/album
new: http://example.com/photos/album

old: http://example.com/gallery/album/subalbum/
new: http://example.com/photos/album/subalbum/

old: http://example.com/gallery/album/subalbum/photo.jpg.html
new: http://example.com/photos/album/subalbum/photo

Upvotes: 1

Views: 267

Answers (2)

4sha
4sha

Reputation: 326

Try:

RedirectMatch 301 ^/gallery(.*?)(\.(jpe?g|gif|png)\.html)?$ /photos$1

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143926

Or alternatively using mod_rewrite:

RewriteRule ^/?gallery/([^/]+)/([^.]+)\.(jpe?g|gif|png)\.html$ /photos/$1/$2 [L,R=301]

You don't need the QSA flag as query strings will automatically get appended if you don't have a ? in your rule's target.

Upvotes: 0

Related Questions