.htaccess redirect with fragment

2019-01-29 09:29发布

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条回答
做自己的国王
2楼-- · 2019-01-29 10:07

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.

查看更多
登录 后发表回答