使用正则表达式的htaccess动态目标

时间:2015-11-02 06:56:08

标签: regex wordpress apache .htaccess

您好我想创建一个删除网址上第一个目录的规则,请参阅示例:

请求网址:http://www.example.com/San-Salvador/help

我想将其重定向到

目标网址:http://www.example.com/help

模式是[base url] [city name] [directory],我想重新创建它[base url] [目录名]

2 个答案:

答案 0 :(得分:0)

这是一个应该有效的一般正则表达式:

^(http:\/\/www\.example\.com\/)(.*\/)(.*)

括号中的每个术语都是,它可能与输入字符串匹配。对于输入字符串:

http://www.example.com/San-Salvador/help

以下是匹配组:

http://www.example.com/
San-Salvador/
help

您要保留的群组是第一个和第三个群组,即http://www.example.com/help为您提供http://www.example.com/help

您可以浏览此正则表达式here at Regex 101

答案 1 :(得分:0)

在大多数情况下,这是一个可以用来删除城市的规则。但是,您的代码需要处理请求重定向URL后发生的情况。表示调用http://www.example.com/help

时显示的内容
RewriteEngine on
RewriteRule ^(?:[^/]+)/([^/]+)/?$ /$1 [L]