RewriteCond RewriteRule for .htaccess 基于新页面的 URL、正则表达式

发布于 2024-12-13 22:42:29 字数 816 浏览 0 评论 0原文

我有一个有趣的 RewriteRule,但我不确定是否制作正则表达式。

简而言之,我想根据内容检查第二个 / - 之后的 URL,可能是转发。

总结起来就是这两条规则:

  1. 如果第二个 / 之后的字符串包含任何字母,则重定向
  2. 如果第二个 / 之后的字符串超过 7 个字符,则重定向

以下是示例:

http://www.mysite.org/section/subsection/2011(好的,什么也不做)

http://www.mysite.org/section/subsection/2011-11(确定,什么都不做)

http://www.mysite.org/section/subsection/2011-11-09(不行,重定向)

http://www.mysite.org/section/subsection/2011-11-W1(不行,重定向)

请帮助我重写规则和重写条件

I have an interesting RewriteRule and I am not sure to make the regular expression.

In short I would like to examine the URL after the second / - based on the contents, possibly forward.

It sums down to these two rules:

  1. Redirect if string after second / contains any letter
  2. Redirect if string after second / is longer than 7 characters

Here are examples:

http://www.mysite.org/section/subsection/2011 (OK, do nothing)

http://www.mysite.org/section/subsection/2011-11 (OK, do nothing)

http://www.mysite.org/section/subsection/2011-11-09 (NOT OK, redirect)

http://www.mysite.org/section/subsection/2011-11-W1 (NOT OK, redirect)

Please help me with the Rewrite Rule and Rewrite Cond

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-12-20 22:42:29

我根据您的示例构建了以下正则表达式,该示例似乎分析了最后一个“/”而不是第二个“/”之后的所有内容。

/[^A-Za-z]{1,7}$

用简单的英语来说,此正则表达式匹配“/”后面的一组字符,并包含 1-7 个非字母字符。为了使其仅匹配最后一个“/”,我附加了一个“$”,它与行尾匹配。

另外,由于您正在处理 URL,因此您可能需要添加“/?”在“$”之前,以防末尾有一个悬空的“/”。

/[^A-Za-z]{1,7}/?$

我希望这能回答你的问题。

I based the following regex off of your examples which seemed to analyze everything after the last '/' instead of the second.

/[^A-Za-z]{1,7}$

In plain English, this regex matches a group of characters that follows a '/' and contains 1-7 non-alphabetical characters. To make it only match the last '/' I appended a '$', which matches the end of a line.

Also, since you are dealing with URLs you might want to add "/?" before the '$' in case there is a dangling '/' at the end.

/[^A-Za-z]{1,7}/?$

I hope this answers your question.

蓝海似她心 2024-12-20 22:42:29

正如莱恩·奥森(Lane Aasen)指出的那样,您的示例和规则不匹配。我以您的示例表示规则应适用于第三条路径段,并提出以下建议。您没有说明重定向目的地是否不同,具体取决于哪个规则匹配。我认为它是不同的。

RewriteEngine on
# redirect if any letter is contained in 3rd part of path
RewriteRule ^([^/]+/){2}.*[a-zA-Z] http://www.bbc.co.uk [R=permanent,L]
# redirect if there are more than 7 characters in 3rd part of the path
RewriteRule ^([^/]+/){2}.{7,} http://www.ibm.com [R=permanent,L]

As Lane Aasen points out, your examples and rules don't match up. I'm taking your examples to mean that the rules should apply to the 3rd path segment, and suggest the following. You don't say whether the redirect destination is different or not depending which rule matches. I've taken it to be different.

RewriteEngine on
# redirect if any letter is contained in 3rd part of path
RewriteRule ^([^/]+/){2}.*[a-zA-Z] http://www.bbc.co.uk [R=permanent,L]
# redirect if there are more than 7 characters in 3rd part of the path
RewriteRule ^([^/]+/){2}.{7,} http://www.ibm.com [R=permanent,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文