如何使用htaccess将url重写为html锚标记(#)

发布于 2024-09-02 02:46:43 字数 386 浏览 4 评论 0原文

我遇到一种情况,我想获取以下 URL:

/1/john

并使用 Apache 的 htaccess 文件将其重定向到

/page.php?id=1&name=john#john

,以便它转到 html 锚点以约翰的名字。

我发现了很多关于转义特殊字符和添加 [NE] 标志以便重定向忽略 # 符号的参考,但这些不起作用。例如,添加 [NE,R] 表示该 URL 只是在浏览器地址中显示为原来的: http://example.com/page.php?id=1&name=john#john

I have a situation where I want to take the following URL:

/1/john

and have it redirect using Apache's htaccess file to go to

/page.php?id=1&name=john#john

so that it goes to an html anchor with the name of john.

I've found a lot of reference to escaping special characters, and to adding the [NE] flag so that the redirect ignores the # sign, but these don't work. For example, adding [NE,R] means that the URL just appears in the browser address as the original: http://example.com/page.php?id=1&name=john#john.

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

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

发布评论

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

评论(4

岁月静好 2024-09-09 02:46:43

这可以使用[NE]标志(noescape)来实现。

默认情况下,特殊字符,例如 &例如, 和 ? 将被转换为其等效的十六进制代码。使用 [NE] 标志可以防止这种情况发生。

更多信息 http://httpd.apache.org/docs/2.2/rewrite /flags.html#flag_ne

This is possible using [NE] flag (noescape).

By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.

More info http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne

乖乖哒 2024-09-09 02:46:43

您可以执行其中一项操作,但不能同时执行两项操作。

您可以使用 [NE] 标志向 Apache 表示不要转义“#”字符,但为了使重定向正常工作,您必须指定要重定向到的绝对 URL,而不仅仅是相对页面。 Apache 无法为您将窗口向下滚动到锚点。但如果您重定向到绝对 URL,浏览器就会这样做。

You can do one of these things, but not both.

You can use the [NE] flag to signify to Apache not to escape the '#' character, but for the redirect to work, you have to specify an absolute URL to redirect to, not simply a relative page. Apache cannot do the scrolling of the window down to the anchor for you. But the browser will, if you redirect to an absolute URL.

小霸王臭丫头 2024-09-09 02:46:43

你想要做的事情,可以通过 URL 重写,或者更具体地说,URL 美化来完成。

我刚刚很快为您找到了这篇解释清楚的博客文章,希望它能有所帮助您将学习学习重写 URL 部分

至于 #-thing(希望您现在知道我在说什么),我认为将同一个变量传递给重写的 URL 两次不会有问题。就像:(注意第一行的最后一部分)

RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/#$2 [R]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&subpage=$2

但是,您必须转义 #-部分,并且 似乎可以这样完成:

RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/\%23$2 [R,NE]

顺便说一句,URL重写并不那么难(但可能会变得复杂,我是不是专家),但 Google 可以在此过程中提供很多帮助。

What you want to do, can be accomplished with URL rewriting, or, more specifically, URL beautification.

I just quickly found this well explained blog post for you, I hope it can help you out with the learning to rewrite URLs-part.

As for the #-thing (expecting that you now know what I'm talking about), I don't see a problem in passing the same variable to the rewritten URL twice. Like: (notice the last part of the first line)

RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/#$2 [R]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&subpage=$2

Though, you'll have to escape the #-part, and it seems that it can be done this way:

RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/\%23$2 [R,NE]

BTW, URL rewriting is not that hard (but can become complicated, and I'm not an expert), but Google can help a lot along the way.

你的心境我的脸 2024-09-09 02:46:43

您无法对锚点进行内部重定向。 (想一想:Apache 如何向下滚动到锚点?)您的链接应该指向 /1/john#john。锚点不是请求 uri 的一部分。

You cannot do an internal redirect to an anchor. (Just think about it: how would Apache scroll down to the anchor?) Your link should pointo to /1/john#john. Anchors aren't part of the request uri.

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