Forum Moderators: phranque

Message Too Old, No Replies

apache rewrite can not match "#"

         

Petergmw

5:06 am on Sep 26, 2018 (gmt 0)

5+ Year Member



Hi all, can somebody help me to find a way to match the special character "#" in source url?
For example, the source url is : https://www.example.com/al/bl/cl#dl/el, I want to redirect it to : https://www.example.com/fl/gl
I was tried to use the following redirect condition, unfortunately, they are all can not work for me. thanks in advance.
1,
RewriteCond %{HTTP_HOST} ^.*example.com$
RewriteRule ^/al/bl/cl#dl/el$ /fl/gl? [NE,R=301,L]
2,
RewriteCond %{HTTP_HOST} ^.*example.com$
RewriteRule ^/al/bl/cl\#dl/el$ /fl/gl? [NE,R=301,L]
3,
RewriteCond %{HTTP_HOST} ^.*example.com$
RewriteCond %{QUERY_STRING} ^/al/bl/cl#dl/el$
RewriteRule ^(.*)$ /fl/gl? [NE,R=301,L]
4,
RewriteCond %{HTTP_HOST} ^.*example.com$
RewriteCond %{QUERY_STRING} ^/al/bl/cl\#dl/el$
RewriteRule ^(.*)$ /fl/gl? [NE,R=301,L]

They are all redirect to :https://www.example.com/fl/gl#dl/el, rather than redirec to https://www.example.com/fl/gl

phranque

8:27 am on Sep 26, 2018 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], Petergmw!

everything after the # in the url is a document fragment identifier and isn't sent by the browser with the request for that url.

Architecture of the World Wide Web, Volume One
3.2.1. Representation types and fragment identifier semantics
https://www.w3.org/TR/webarch/#media-type-fragid
Interpretation of the fragment identifier is performed solely by the agent that dereferences a URI; the fragment identifier is not passed to other systems during the process of retrieval.

Petergmw

9:16 am on Sep 26, 2018 (gmt 0)

5+ Year Member



Hi Phranque, thanks for your help. but the string append to the target url will reduce the customer satisfaction which the string is everything after the "#". and it's no meaning for the target url.
For example the url : https://www.example.com/al/bl/cl#dl/el --the fragment identifier showing here is make sence; but if showing at https://www.example.com/fl/gl#dl/el --then not make sence.

Thanks
Peter G

lucy24

4:51 pm on Sep 26, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You may have misunderstood. The server cannot see anything after # because that part is simply not sent to the server in the first place. It is only used by the browser.

You can use # in a target (“redirect to suchandsuch specific location on the page")--that's where the [NE] flag comes in--but you cannot use it in a pattern.

The character # can occur in a query string--but be careful, because it may be percent-encoded. That would be a different rule. The sample URL in your original question doesn't contain a query string, though.

Petergmw

4:58 am on Sep 27, 2018 (gmt 0)

5+ Year Member



Hi Lucy24, thanks for your help. according your reply, we have no way to drop the append string which the string is after "#", right?

lucy24

5:26 pm on Sep 27, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Correct--sort of. In your initial post, you said
For example, the source url is : https://www.example.com/al/bl/cl#dl/el, I want to redirect it to : https://www.example.com/fl/gl
Now, even without the # part, those are still two different URLs. The request the server receives is
https://www.example.com/al/bl/cl
and then the user's browser looks on the page for a fragment (either <a name> or anything with <id>) called
dl/el
If the browser doesn't find any such fragment, it just stays at the top of the page.

So you can't redirect requests for /al/bl/cl#dl/el -- because they won't exist -- but you can certainly redirect requests for /al/bl/cl if that is any help.

Come to think of it, what does the server do with a request for /al/bl/cl ? There's no extension and no directory slash, so it will have to perform some kind of rewriting. Is there a CMS (either homemade or a standard package)?

Petergmw

12:18 am on Sep 29, 2018 (gmt 0)

5+ Year Member



Hi Lucy24, thanks very much for your help, I totally understand now.
the example just using for quick/easy for example, the actually url is not like that. but they have exactly theroy.
Thanks again.

phranque

1:03 am on Sep 29, 2018 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The character # can occur in a query string--but be careful, because it may be percent-encoded.

not "may be percent-encoded" - must be percent-encoded

https://tools.ietf.org/html/rfc3986#section-2.2

lucy24

3:54 am on Sep 29, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When I said “may be” (“might be” would have been less ambiguous) I meant that at the point when the query string reaches a RewriteCond in your htaccess file, any # it contains may currently be expressed as %23, so you’d need to write your pattern that way.

:: detour to mess around on test site, which I didn’t have time to do earlier ::

Huh. I couldn't get it to admit seeing a # in a query string either as # or as %23. It does see it as something: if I tell it to capture the whole query string and show it to me, it’s perfectly happy to do so with a query string that’s got a # in it.

:: generic puzzlement, leading to predictable conclusion that it’s best not to use # as anything but a properly constituted fragment identifier ::