.htaccess redirect with fragment

烂漫一生 提交于 2019-12-18 09:38:57

问题


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 #.

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.

Ideally I should be able to use QSA as well.

For example:

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

should become

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

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]

回答1:


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]

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.



来源:https://stackoverflow.com/questions/371791/htaccess-redirect-with-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!