Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess RewriteRule to encode ®

I need to rewrite any ® in the url parameters to %C2%AE (UTF-8) by using the RewriteRule in the .htaccess file. I need this because Internet Explorer is not accepting this special character, other browsers are. It is not possible to change the url, since it is generated outside my reach.

Example of a url to be rewritten is:

https://www.example.com/training/some-training/subscribe/?t=Some+Training®&id=81

This needs to be rewritten to:

https://www.example.com/training/some-training/subscribe/?t=Some+Training%C2%AE&id=81

The ® can happen anywhere in the parameters. If possible, preferably it only needs to be rewritten on Internet Explorer, but perhaps that is just wishful thinking.

Is there anyone that can help me with the RewriteRule expression to use for this case? Thank you.

like image 899
Femke Avatar asked Nov 23 '25 08:11

Femke


1 Answers

I need this because Internet Explorer is not accepting this special character, other browsers are.

If IE was "not accepting this special character" then there probably wouldn't be much you could do (as noted in my comment). However, in my tests on IE11, it seems that IE is "simply" not URL encoding the ® symbol when making the request*1. Other browsers are helping out and "fixing" the URL by URL-encoding this char as %C2%AE as part of the request.

(*1 although if the request is refereshed in IE then IE seems to be substituting the ® symbol for U+FFFD "Replacement Character" - despite the dev tools showing the ® symbol?!)

Providing the request is reaching your server then this can be fixed. (Although the request is strictly invalid.)

If the request is reaching your application then you should also be able to handle this in the application code itself - but maybe you do not have control over this?

Try something like the following at the top of your .htaccess file to "redirect" (not "rewrite") the request if it contains an unencoded ® symbol in the query string part of the requested URL, replacing this with %C2%AE in the redirection response:

RewriteEngine On

RewriteCond %{QUERY_STRING} (.+)®(.*)
RewriteRule ^training/some-training/subscribe/$ /$0?%1\%C2\%AE%2 [R,L,NE]

The ® symbol can be represented literally in the CondPattern.

$0 is a backreference to the entire URL-path - to save typing.

%1 and %2 are backreferences to the string before and after the ® symbol.

\%C2\%AE - note the backslash escaped % characters to represent literal % to avoid any confusion with backreferences of the form %n. (Although it should be OK without in this instance, it is better to be explicit.)

The NE (noescape) flag prevents the susbstitution string being doubly encoded.

preferably it only needs to be rewritten on Internet Explorer

It would seem to be only IE that is sending this character unencoded in the first place. But you would presumably need to encode this character regardless of browser, since it is presumably your application that is breaking because of it?

UPDATE: ...Now it is 'training/some-training/subscribe/', but the url does not always have this text, it could be anything.

If the URL-path can be anything then let it match anything:

RewriteCond %{QUERY_STRING} (.+)®(.*)
RewriteRule .* /$0?%1\%C2\%AE%2 [R,L,NE]

However, it is better to be as restrictive as possible, since this will now be processed on every request.

shouldn't the character to be rewritten (®) in the first part after RewriteRule?

The RewriteRule pattern (the first argument to the RewriteRule directive) matches against the URL-path only - this notably excludes the query string portion of the URL. This is why we need to use an additional RewriteCond (condition) directive to check (and capture) the query string portion of the URL.

like image 124
MrWhite Avatar answered Nov 26 '25 02:11

MrWhite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!