1

Need help with rewriting part of query string with mod_rewrite.

Looked through a lot of resources and have a general understanding of how stuff works but cannot figure out correct solution

This link:

http://example.com/?param=home&shop_id=1000005620&ate=bow&b_uid=-1&tg=one

Has to become this

http://example.com/?param=home/#/shop/1000005620?ate=bow&b_uid=-1&tg=one


If shorter, then this part of query string

&shop_id=1000005620& transforms into /#/shop/1000005620?


UPDATE:

Answer gave me a clear understanding what I had to do. Exact rules that fixed my issue were like this:

<IfModule mod_rewrite.c>
    RewriteCond %{QUERY_STRING} ^(.*)&shop_id=([0-9]{10,12})(?:&)(.*)$
    RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1?%1/#/shop/%2\?%3 [NE,L,R]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{QUERY_STRING} ^shop_id=([0-9]{10,12})$
    RewriteRule ^(.*)$ https://%{SERVER_NAME}/#/shop/%1? [NE,L,R]
</IfModule>

Reason for this rewrite rules is hash handling with safari and ie I have links that are used by external pages and also I have a redirection to https if site request http. If there is a hash in the link and request is sent from Safari or IE hash goes away from the URL and does not come back after redirection. I want to note very important fact that Chrome, Firefox do not have problems with keeping the URL with hash even after redirect to https. This involved to reconstruct our URL but it is worth it and now everything is working as it should.

1 Answer 1

3

You can try:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&shop_id=([^&]+)&?(.*)$
RewriteRule ^(.*)$ /$1?%1/#shop_id=%2?%3 [L,R,NE]

EDIT:

what about if I only what to rewrite http://example.com/?shop_id=1000005620 into http://example.com/#/shop/1000005620 what the rewrite rule be then?

Just change the relevant parts of the regex pattern:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^shop_id=([^&]+)$
RewriteRule ^(.*)$ /$1/#shop_id=%2? [L,R,NE]
Sign up to request clarification or add additional context in comments.

3 Comments

what about second & in &shop_id=1000005620& in the end, how to replace it with ? or not to match in rewriteCond so that in the end I will get ? like this /#/shop/1000005620?.
what about if I only what to rewrite http://example.com/?shop_id=1000005620 into http://example.com/#/shop/1000005620 what the rewrite rule be then?
Thank big time!!!!! I will post update on exact solution that I needed. It was part of solving problem with Safari and IE handling redirects with # in url. thank you one more time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.