本文介绍了.htaccess中:正则表达式匹配替换的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想htaccess的重定向301做使用正则表达式如下:

I want the htaccess Redirect 301 to do the following using regex:

http://example.com/folder/abc_123_123.htm

http://example.com/abc-123-123.shtml

新的URL的3目标是

  • 删除/文件夹/

  • delete /folder/

替换所有_用 -

替换HTM用的shtml

replace htm with shtml

推荐答案

最好的我知道,你点#2不能没有的,这需要一些讨厌的配置,或多个规则。

Best I'm aware, your point #2 cannot be done without a RewriteMap, which requires some pesky configuration, or multiple rules.

本实施应该是通用的,并为任意数量的下划线的工作,但它的价格昂贵,因为它可能会引发很多的重定向(实际上,可能足以火在浏览器重定向错误,如果你已经吨下划线在你的路径):

This implementation should be generic and work for any number of underscores, but it's expensive in that it might trigger many redirect (in fact, potentially enough to fire redirect errors in browsers if you've tons of underscores in your paths):

RewriteBase /
RewriteRule ^(folder/[^_]*)_(.*\.html?)$ /$1-$2 [L,R=301]
RewriteRule ^folder/(*+)\.html?$ /$1.shtml [L,R=301]

根据需要,或

替代品包括列举的用例(好,我怀疑)重写请求到Perl或PHP脚本,做REGEXP_REPLACE +从那里重定向301。

Alternatives include enumerating use-cases as needed, or (better, I suspect) rewriting the request to a perl or php script and doing the regexp_replace + redirect 301 from there.

RewriteBase /
RewriteRule ^folder/(*+)\.html?$ /folder/redirect.php [L,QSA]

这篇关于.htaccess中:正则表达式匹配替换的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:04