mod_rewrite RewriteRule 处理 HTML 实体

发布于 2024-09-09 23:12:41 字数 447 浏览 2 评论 0原文

由于某种原因,我们在包含 HTML 实体的 URL 上收到大量入站流量。

例如

http://www.ourdomain.com/index.php?key1=value1&key2=value2

我想创建一个 RewriteRule 来替换这些 HTML 实体(特别是 & 与 & ;),并将人们转发到更正的地址。

我尝试过这些:

RewriteRule & & [R=permanent]

RewriteRule & &

但什么也没发生。

(顺便说一句。我知道这是解决别人问题的一种可怕且危险的方式,但我只需要暂时完成它)

感谢您的帮助...

For some reason, we recieving a bunch of inbound traffic on URLs that contain HTML entities.

E.g.

http://www.ourdomain.com/index.php?key1=value1&key2=value2

I want to create a RewriteRule to replace these HTML entities (specifically & with &), and forward people on to the corrected address.

I'm tried these:

RewriteRule & & [R=permanent]

and

RewriteRule & &

But nothing is happening.

(BTW. I know this is a horrible and dangerous way to solve someone else's problem, but I just need to get it done. temporarily)

Thanks for any help...

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

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

发布评论

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

评论(2

倾城°AllureLove 2024-09-16 23:12:41

这应该可以解决问题;

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)&(.*)
RewriteRule .* /index.php?%1&%2 [N,R=301]

这实质上是说;

  • 查询字符串中是否有 &
  • 如果是,则匹配其之前和之后的所有内容,然后用 & 将它们夹在一起。
  • 规则中的 N 标志实际上表示“继续执行此操作,直到条件不再满足”匹配'

查看 mod_rewrite 文档,包括 RewriteCond规则标志

This should do the trick;

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)&(.*)
RewriteRule .* /index.php?%1&%2 [N,R=301]

This essentially says;

  • Is there an & in the query string?
  • If yes, match everything before and after it, then sandwich them together with an &
  • The N flag in the rule effectively says 'keep doing this until the condition no longer makes a match'

Check out the mod_rewrite documentation, including RewriteCond and rule flags.

匿名。 2024-09-16 23:12:41

为了使任何重写工作,重写引擎必须打开,但也许您已经修复了这个问题:

# following statement must come before any other rewrite statements
# and must be enabled per virtual host configuration
RewriteEngine On

& 符号是一个特殊的元字符。显然,复制并粘贴您的链接的人是在编辑器中进行了此操作,该编辑器适当地将&符号转义为 & (或者,从技术上讲更正确的是,&) code> 如果它发生在 a 标签内)。我不是 100% 确定,但我发现 & 符号应该被转义,试试这个:

RewriteRule \& \&

要了解重写如何进行、重写什么以及为什么,您应该打开重写日志记录(确保 Apache 可以写入RewriteLog的位置):

RewriteLog "somepath.log"
RewriteLogLevel 3

其中级别 9 是最高的。

For any rewrite to work, the rewrite engine must be on, but perhaps you already have that fixed:

# following statement must come before any other rewrite statements
# and must be enabled per virtual host configuration
RewriteEngine On

The ampersand is a special metacharacter. Apparently the people that have copied and pasted your links did so inside an editor which duely escaped the ampersands into & (or, more technically correct, & if it happened inside an a-tag). I'm not 100% sure, but I find it likely that the ampersand should be escaped, try this:

RewriteRule \& \&

To find out how the rewrite goes, what it rewrites and why, you should turn rewrite logging on (make sure Apache can write to the location of RewriteLog):

RewriteLog "somepath.log"
RewriteLogLevel 3

where level 9 is the highest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文