Apache mod_rewrite 添加 anchor 到查询字符串

标签 apache .htaccess redirect mod-rewrite url-rewriting

我正在尝试使用查询字符串向 URL 添加 anchor 。原网址为

https://example.com/foo?query=123

它应该重定向到

https://example.com/foo?query=123#anchor

这些是我在 .htaccess 文件中的规则:

RewriteCond %{REQUEST_URI} ^/foo$
RewriteCond %{QUERY_STRING}  ^query=[a-zA-Z0-9\s]*$
RewriteRule (.*) /foo?%{QUERY_STRING}#anchor [NC,NE,R=302,L,END]

您知道如何避免重定向循环吗?

最佳答案

根据您展示的示例,您能否尝试以下操作。 请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([\w-]+\?query=\d+)\s [NC]
RewriteRule ^ %1#anchor [L,R,NE]

关于Apache mod_rewrite 添加 anchor 到查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66900121/

相关文章:

php - UTF-8贯穿始终

algorithm - 谷歌算法的文件结构

debugging - 如何调试 htaccess 重写脚本

.htaccess - 将某些页面的 https 重定向到 http

apache - RewriteCond 以任意顺序匹配查询字符串参数

php - 在 nginx 中从 robots.txt 重定向到 robots.php

php - 是否可以在 apache 启动时启动 php 脚本?

PHP header Location 在 HTML 中间工作

apache - CSS、JS 和图像不显示漂亮的 url

.htaccess 强制在 Safari 中下载 PDF