views:

764

answers:

2

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]
A: 
RewriteRule /test http://www.example.com/#MYVALE [R,QSA,NE]
Vinko Vrsalovic
That's close - the # comes out ok but the URL ends up as /#MYVALUE?foo=bar
Greg
Greg
+1  A: 

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.

Greg