Apache重写从URL中删除.jpg.html

时间:2013-09-06 19:14:33

标签: regex .htaccess mod-rewrite url-rewriting gallery

我一直在尝试为apache写一个重写规则,将我的gallery2网址切换为gallery3网址格式:

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

请注意,在上面的URL示例中,“/ photoalbumxyz / photo.jpg.html”不是实际的物理目录,它只是gallery2重写“友好”URL的方式。我可以使用如下规则重写/ gallery / to / photos /:

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

但是如果它与/ gallery / - >组合存在,我无法找出匹配和删除“.jpg.html”扩展名。 / photos / rewrite。我认为正则表达式匹配将是.jpg.html以逃避期间,但如何编写规则以删除“.jpg.html”扩展并重写目录?

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

编辑: 抱歉!我之前忽略了提到专辑URL格式可以更改(不必指定照片,并且可以包含子专辑),我添加了一些具体的例子:

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

2 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:0)

或者使用mod_rewrite:

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

如果您的规则目标中没有QSA,则不需要?标记,因为查询字符串会自动附加。