301 重定向与 RegEx
301 redirect with RegEx
我在这里搜索了正确的表达方式,但似乎找不到。我需要重写几十个 URL,并想使用正则表达式将所有 301 重定向到一个页面。
当前页面都是这样的:
www.example.com/2008/07/garbage.html/this-is-what-i-want
www.example.com/2005/09/moregarbage.html/this-is-what-i-want
我想把所有东西都从中间切下来,然后把两页都拿去
www.example.com/this-is-what-i-want
您似乎想要从第一个/到最后一个/删除所有内容...
我不知道你使用的是什么语言,但像这样的东西可以作为正则表达式来匹配你想要匹配的内容。
((https?://)?[^/]+)/.+(/[^/]+)
第一个和第三个匹配组包含您想要的部分...例如,在 Python:
import re
str = 'http://www.example.com/test/this.com/this-is-what-I-want'
regexp = r'((https?://)?[^/]+)/.+(/[^/]+)'
match = re.match(regexp, str)
print match.group(1) + match.group(3)
产量:
$ python t.py
http://www.example.com/this-is-what-I-want
在您的主 WP .htaccess 中,您可以在 RewriteEngine
行下方使用此重写规则:
RewriteEngine On
RewriteRule ^\d{4}/.*/([^/]+)/?$ / [L,R=301]
我在这里搜索了正确的表达方式,但似乎找不到。我需要重写几十个 URL,并想使用正则表达式将所有 301 重定向到一个页面。
当前页面都是这样的:
www.example.com/2008/07/garbage.html/this-is-what-i-want
www.example.com/2005/09/moregarbage.html/this-is-what-i-want
我想把所有东西都从中间切下来,然后把两页都拿去
www.example.com/this-is-what-i-want
您似乎想要从第一个/到最后一个/删除所有内容...
我不知道你使用的是什么语言,但像这样的东西可以作为正则表达式来匹配你想要匹配的内容。
((https?://)?[^/]+)/.+(/[^/]+)
第一个和第三个匹配组包含您想要的部分...例如,在 Python:
import re
str = 'http://www.example.com/test/this.com/this-is-what-I-want'
regexp = r'((https?://)?[^/]+)/.+(/[^/]+)'
match = re.match(regexp, str)
print match.group(1) + match.group(3)
产量:
$ python t.py
http://www.example.com/this-is-what-I-want
在您的主 WP .htaccess 中,您可以在 RewriteEngine
行下方使用此重写规则:
RewriteEngine On
RewriteRule ^\d{4}/.*/([^/]+)/?$ / [L,R=301]