从不带哈希值的 URL 重定向到带哈希值的 URL
在 Apache 上,我想使用 .htaccess 创建 URL 重定向规则。
预期的传入 URL 将根据以下模式形成:
[PROTOCOL]://[domain name]/i/[INTEGER]/[INTEGER]/[STRING]
我希望上述 URL被重定向,以便 URL 中现在有一个哈希值,如下所示:
[PROTOCOL]://[domain name]/i/
#/[INTEGER]/[INTEGER]/[STRING]
示例
我想要的 ..
从此:
https://example.com/i/92/17/abcdef
至此:
https://example.com/i/#/92/17/abcdef
重要:浏览器不得删除重定向 URL 中的哈希值。 我需要该哈希值。
我为什么要这样做?因为原始传入 URL(不带哈希值)的路由实际上并不存在。我需要 javascript 来检查哈希后出现的项目,并使用它们从浏览器向 API 服务器发出获取请求。这是必要的,因为我的主机上只有静态 HTML 文件。另外,我想要一个美观的传入 URL,它不包含主题标签(即使它将被重定向到带有主题标签的 URL)。
问题是,如何使用 https://example.com/i/
子目录中的 .htaccess
文件实现此目的?
On Apache, I want to create a URL redirect rule using .htaccess.
The expected incoming URL will be formed according to the following pattern:
[PROTOCOL]://[domain name]/i/[INTEGER]/[INTEGER]/[STRING]
I want the above URL to be redirected so that there is now a hash in the URL, as follows:
[PROTOCOL]://[domain name]/i/
#/[INTEGER]/[INTEGER]/[STRING]
Examples
I want to go...
FROM THIS:
https://example.com/i/92/17/abcdef
TO THIS:
https://example.com/i/#/92/17/abcdef
Important: the browser must not strip out the hash in the redirected URL. I need that hash.
Why would I want to do this? Because routes for the original incoming URLs (without the hash) don't actually exist. I need javascript to examine the items that appear after the hash, and use them to make fetch requests to an API server from the browser. This is necessary because my host will only have static HTML files on it. Also I want a cosmetically-pleasing incoming URL which does not contain a hashtag (even though it will be redirected to a URL with a hashtag).
The question is, how do I achieve this with an .htaccess
file that lives in the https://example.com/i/
subdirectory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
/i
子目录中的.htaccess
文件顶部使用 mod_rewrite:由于
.htaccess
文件位于/i
子目录中,RewriteRule
模式(第一个参数)与[INTEGER]/[INTEGER]/[STRING]
部分匹配URL 路径(即相对于包含.htaccess
文件的目录)。$0
反向引用包含与RewrietRule
模式 匹配的整个 URL 路径。需要
NE
标志来防止重定向响应中的#
被 URL 编码(如%23
)。参考:
Using mod_rewrite at the top of the
.htaccess
file in the/i
subdirectory:Since the
.htaccess
file is in the/i
subdirectory, theRewriteRule
pattern (first argument) matches against the[INTEGER]/[INTEGER]/[STRING]
part of the URL-path (ie. relative to the directory that contains the.htaccess
file).The
$0
backrereference contains the entire URL-path that is matched by theRewrietRule
pattern.The
NE
flag is required to prevent the#
being URL-encoded (as%23
) in the redirect response.Reference: