重定向到新类型的网址并删除部分网址,但有异常

时间:2015-05-29 09:24:34

标签: regex .htaccess mod-rewrite redirect url-redirection

我需要将旧链接重定向到新链接。新链接会有所不同。

例如这个 (我在不属于帖子名称的部分标记了capslook)

http://www.example.com/VIEW-what-ever-dynamic-url-that-changes-RESOLUTION.html

(“ - RESOLUTION”类似于“-1400x1050”或“-other”等。)

将重定向到:

http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/what-ever-dynamic-url-that-changes-RESOLUTION.jpg

注意旧网址中的“view-”消失,“。html”替换为“.jpg”(图片类型)

所以,如果RESOLUTION为-1400x1050,那么新链接也是如此。

http://www.example.com/VIEW-what-ever-dynamic-url-that-changes-1400x1050.html

http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/what-ever-dynamic-url-that-changes-1400x1050.jpg

异常:

http://www.example.com/VIEW-what-ever-dynamic-url-that-changes-RESOLUTION.html

如果“-RESOLUTION”(如果它结束)“ - other”或“-normal”,请将其从网址中删除

示例:

http://www.example.com/VIEW-what-ever-dynamic-url-that-changes-other.html

将重定向到

http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/what-ever-dynamic-url-that-changes.jpg

http://www.example.com/VIEW-what-ever-dynamic-url-that-changes-normal.html
will

重定向到

http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/what-ever-dynamic-url-that-changes.jpg

到目前为止,我有这个,但只有具有“example.com/view-”的网址才需要重定向到该网址,而不是所有网址。也许你可以指出我正确的方向?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite\.com$ [NC]

RewriteRule ^/(.*)-other.html$ http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/$1.jpg [R=301,L]
RewriteRule ^/(.*)-normal.html$ http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/$1.jpg [R=301,L]

1 个答案:

答案 0 :(得分:0)

试试这个.htaccess:

RewriteEngine on

# other or normal URL
RewriteRule ^VIEW-(.+?)-(?:other|normal)\.html$ http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/$1.jpg [R=302,L,NC]

# resolution URL
RewriteRule ^VIEW-(.+?\d+x\d+)\.html$ http://example.com/wp-content/show-image.php?path=http://example.com/wp-content/uploads/$1.jpg [R=302,L,NC]