Ad

.htaccess Redirect With Fragment

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]
Ad

Answer

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.

Ad
source: stackoverflow.com
Ad