Apache .htaccess:重定向到包含主题标签的 URL

Apache .htaccess: Redirect to an URL containing a hashtag

URL

https://example.com/moved/

https://www.example.com/moved/

应该重定向到 URL

https://my.example.com/#views/settings.php?id=2

通过 .htaccess.

这是我试过的:

RewriteCond %{HTTP_HOST} ^example.com
RewriteCond %{REQUEST_URI} moved$
RewriteRule (.*)$ https://my.example.com/#views/settings.php?id=2 [R=301,L]

不过好像不行。我想原因是我想重定向到的 URL 中的 #hashtag(它在 .htaccess 中标记了一条评论)。

如何正确重定向?

将您的规则更改为:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^moved/?$ https://my.example.com/#views/settings.php?id=2 [R=301,L,NE,NC]
  • ^(?:www\.)?example\.com$ 将匹配 www.example.comexample.com
  • 标志 NE 用于 在重定向 URL
  • # 的无转义
  • 模式 moved/?$ 匹配 /moved//moved,因此尾部斜杠是可选的。

确保清除浏览器缓存或使用新浏览器进行测试。