需要在 .htaccess 重写规则中转义 #(哈希/井号)字符

发布于 2024-09-15 23:27:26 字数 321 浏览 4 评论 0原文

这个问题相当简单,但我几个小时都找不到答案。

我需要做的是:

RewriteRule ([^#])#(.*) $1\%23$2

这基本上意味着我想 url 转义从外部代码片段中得到的奇怪的哈希符号。

反斜杠 (\) 无法转义此符号...并且请不要建议使用 %23 代替 #,因为它不会工作也一样。

%23 与 # 不匹配,因为它根本不是 == %23

The question is fairly simple but I was not able to find an answer for hours now.

What I need to do is:

RewriteRule ([^#])#(.*) $1\%23$2

Which basically means I want to url escape the freaking hash sign which comes to me from an external codepiece.

backslash (\) does not work to escape this sign... and please don't suggest using %23 instead # because it does not work as well.

(%23 does not match a # because it simply is not == %23)

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

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

发布评论

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

评论(4

长伴 2024-09-22 23:27:26

我刚刚在这个论坛上发布了几篇帖子后,在一个网站上实现了此功能,我使用的是 NE 不转义和 R=301 重定向选项的重写规则:

RewriteRule ^galleries/([a-zA-Z0-9_-]+)$ /gallery.html#/$1 [R=301,NE,L]

这会将所有画廊/变量重定向到< em>/gallery.html#/variable

编辑:规则的重要部分是 NE,它指示服务器解析输出而不转义字符。如果没有这个,它将尝试转义重写规则中的#,这正是OP所询问的。

I just got this working for a site following a couple of posts on this forum, I'm using a rewrite rule with NE not escape and R=301 redirect options:

RewriteRule ^galleries/([a-zA-Z0-9_-]+)$ /gallery.html#/$1 [R=301,NE,L]

This redirects all galleries/variable to /gallery.html#/variable

Edit: The important part of the rule is NE which instructs the server to parse output without escaping characters. Without this, it will try and escape the # in the rewrite rule which is what the OP is asking about.

得不到的就毁灭 2024-09-22 23:27:26

URL 的哈希部分不可重写。当 Web 浏览器向 Web 服务器发送 URL 请求时,它会将所有内容发送到哈希符号。哈希值仅在客户端可用(例如 JavaScript 代码可以看到它)。

The hash part of a URL is not available for rewriting. When a web browser sends a URL request to a web server it sends everything up to the hash sign. The hash is only available on the client (e.g. JavaScript code can see it).

我喜欢麦丽素 2024-09-22 23:27:26

http 中搜索扩展重定向 ://httpd.apache.org/docs/2.0/misc/rewriteguide.html。对于你的问题有一个很好的解决方案。

Search Extended Redirection in http://httpd.apache.org/docs/2.0/misc/rewriteguide.html . There is a nice solution for your question.

余生一个溪 2024-09-22 23:27:26

.htaccess

RewriteRule  old\.php redirect.php?url=http://example.com/new.php|hash [R=301,QSA,L]

重定向.php

<?php
    $new_url = str_replace("|", "#", $_GET['url']);
    header("Location: ".$new_url, 301);
    die;
?>

.htaccess

RewriteRule  old\.php redirect.php?url=http://example.com/new.php|hash [R=301,QSA,L]

redirect.php

<?php
    $new_url = str_replace("|", "#", $_GET['url']);
    header("Location: ".$new_url, 301);
    die;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文