Good day everyone, I need help on .htaccess

Two days I can not understand how it works, that just did not try.

To the point, there is a link:

domain.ru/s_shoping/shop?sid=253%23modal/bundle/82522 

It has the character %23 , I need to replace with ( # ) hash. Such links appear for example if you share a link in Skype on ios, maybe in other programs there is the same problem

As I just did not try to write:

 RewriteRule ^(/shop\?sid=[0-9]*)?%23(modal/bundle/[0-9]*)$ /shop\?sid=$1#modal/bundle/$2 [NE,R=301,L] RewriteRule ^(.*)?%23(.*)?$ $1#$2 [NE,R=301,L] 

In general, I tried a dozen variants, with different flags, additional modules, etc. but all is useless ... Where am I making a mistake? If you use one group, then everything works, if two are already broken (or if any character behind the group doesn’t work either) There is nothing special in these regulars ... Or maybe you can do with changing the encoding ...

Thanks in advance for any hint

  • Try RewriteCond %{QUERY_STRING} ^(.*?)%23(.*)$ And immediately after this RewriteRule ^/(s_shoping/shop)/?$ /$1?%1#%2 [R,NE,L] - Wiktor Stribiżew

1 answer 1

If you really need to make a redirect, then use the flag [B] .

_rewrite has unescape URLs before mapping them so they will be unescaped at the time they are applied. Using the flag, it will be escaped.

The % symbol is reserved for referring to captured groups from the RewriteCond section within the RewriteRule section. mod_rewrite treats them differently, so your sequence %23 can be processed as an empty captured group and replaced with an empty string. The [B] flag disables such processing.

To fix this, add this flag to your rules, for example:

 RewriteRule ^(.*)?%23(.*)?$ $1#$2 [NE,R=301,L,B] 

Check out the work here .


In fact, this is not a problem.
These programs encode special characters in the URL in a special way.
In fact, the sequence %23 equivalent to # and you don’t need to do anything about it.

Decode the query string value on the server side.
In PHP, for example, the urldecode() function is responsible for this.

Wikipedia article on this topic: URL coding

The URL standard has a serious drawback - it can only use a limited set of US-ASCII characters, even smaller than in ASCII: Latin letters, numbers, and only a few punctuation marks (- _. ~ +).

All other characters must be recoded, for example, such characters may be Cyrillic letters and accented characters, ligatures, hieroglyphs.

This encoding is described in RFC 2396 and RFC 3986 standards, and is called English. URL-encoding, URLencoded or percent ‐ encoding.

An example of coding can be seen in the Russian-language Wikipedia, using the Russian language in the URL. For example, a string like:

 https://ru.wikipedia.org/wiki/Микрокредит 

encoded as:

 https://ru.wikipedia.org/wiki/%D0%9C%D0%B8%D0%BA%D1%80%D0%BE%D0%BA%D1%80%D0%B5%D0%B4%D0%B8%D1%82 

The answer used information from the answer to the question:
.htaccess url encoded string passing