大网数据广告活动
网站运营与故障问答

Can a URL with a # anchor be redirected in .htaccess?

· 52CE 我爱测

I believe this can't be done, but it would be good to confirm either way. To know if it's worth investing more time in or not. Can a .htaccess file rewrite/redirect: example.net/postname#heading2 be redirected to: example.com/pos…

问题原文

原帖链接 · mvsainz · CC BY-SA 4.0

I believe this can't be done, but it would be good to confirm either way. To know if it's worth investing more time in or not.

Can a .htaccess file rewrite/redirect:

example.net/postname#heading2

be redirected to:

example.com/postname#heading2

?

I'm starting to assume that the server does not see anything past the #. I am also trying to add a UTM parameter after #heading2 in the process. The current rewrite is adding #heading2 appended directly after the UTM paramter.

Any confirmation would be appreciated.

EDIT: Apparently I was understanding the issue wrong due to the combination of anchors with UTM parameters. The URL was properly redirecting via .htaccess, it's just that I was unaware that the #anchor should always go at the end of the URL, even if there's a UTM parameter before it. Reference. Therefore the right URL was always supposed to look like:

example.com/postname/?utm_source=XYZ#heading2

已采纳解答

原帖链接 · Alexis Wilke · CC BY-SA 4.0

No.

The anchor is a browser side only parameter which is not sent to the server.

You either need to transform the #anchor into a query string or a path, or have some JavaScript that understands how to handle that special case.

If you know of the specific anchor, having a specific URL that understands about it would work. But that's similar to placing that parameter in a query string or as part of the path.

Twitter used the anchor capability quite a bit, but all their code used JavaScript to retrieve the data to be displayed. That's why that worked well for them.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。