.htaccess 重定向与片段 [英] .htaccess redirect with fragment

查看:29
本文介绍了.htaccess 重定向与片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 .htaccess 向带有片段(也称为锚点)的 URL 发出重定向,但它会自动转义 #.

I need to issue a redirect, using .htaccess, to a URL with a fragment (also known as an anchor), but it's automatically escaping the #.

目前我想要一个硬编码的片段,但为了其他人,如果你也知道如何从 URL 中获取它,那就太好了.

At the moment I want a hard-coded fragment, but for the sake of others if you know how to take it from the URL too that would be good.

理想情况下,我也应该能够使用 QSA.

Ideally I should be able to use QSA as well.

例如:

http://www.exameple.com/test?foo=bar

应该变成

1) http://www.example.com/?foo=bar#MYVALUE

或从 url 中获取片段:

or taking the fragment from the url:

2) http://www.example.com/?foo=bar#test

我的(非工作)代码如下所示:

My (non-working) code looks like this:

RewriteRule /test http://www.example.com/#MYVALE [R,QSA]

推荐答案

我想我已经弄清楚了...

I think I've got it figured out...

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /test
RewriteRule ^test/?(.*)$ $1 [C]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1?%{QUERY_STRING}#MYVALUE [NE,L,R]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} /test
RewriteRule ^test/?(.*)$ $1 [C]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1?%{QUERY_STRING}#MYVALUE [NE,L,R]

必须为 HTTP 执行一次,为 HTTPS 执行一次,这有点糟糕 - 我会看看是否能找到解决方法.

It's a bit poor having to do it once for HTTP and again for HTTPS - I'll see if I can find a way around that.

这篇关于.htaccess 重定向与片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆