您可以使用 mod_alias 执行此操作:
RedirectMatch 301 ^/.*?/[0-9]+_(.*)\.html?$ /search/result/?q=$1
301
如果您不希望它成为永久重定向,请删除。你也可以使用 mod_rewrite,它看起来几乎一样:
RewriteRule ^/?.*?/[0-9]+_(.*)\.html?$ /search/result/?q=$1 [L,R=301]
=301
同样,如果您不想要永久重定向,则可以删除该位。
编辑:
为了_
用字符替换查询字符串的字符,+
您肯定需要坚持使用 mod_rewrite 并且需要一些额外的规则:
# perform the initial rewrite, but don't redirect
RewriteRule ^/?.*?/[0-9]+_(.*)\.html?$ /search/result/?q=$1 [L]
# replace "_" with "+"
RewriteCond %{QUERY_STRING} ^q=([^_]*)_(.*)$
RewriteRule ^/?search/result/$ /search/result/?q=%1+%2 [L,NE]
# don't redirect until all "_" is replaced with "+"
RewriteCond %{QUERY_STRING} ^q=([^_]+)$
RewriteRule ^/?search/result/$ /search/result?q=%1 [L,R=301]