首页 > 解决方案 > 重定向到锚点

问题描述

我需要将这样的 url 重定向domain.tld/instagram/anchordomain.tld/instagram/#anchor.

这应该只发生在与 instagram 路径的链接上,并且必须通过 .htaccess 处理,我试过这个,现在我被卡住了,因为它什么都不做:

RewriteRule ^/instagram/(\d[^/]+) /instagram/#$1/ [R=301,NE,L]

标签: regex.htaccessredirectmod-rewrite

解决方案


对于您显示的示例,请尝试以下规则。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
RewriteRule ^instagram/([^/]*)/?$ /instagram/#$1/ [R=301,NC,NE,L]

要点:

  • 始终在规则中使用 NE 和 NC 标志,以确保条件格式正确。
  • 您不需要在您的正则表达式中使用 \d ,因为您显示的示例中没有数字。
  • 您需要确保将此规则保持在顶部,因为它应该首先出现重定向,以防以后我们有其他与之相关的规则。
  • 你不需要匹配/从这里开始。

推荐阅读