I
I
igorkorneichuk2020-04-14 11:10:03
htaccess
igorkorneichuk, 2020-04-14 11:10:03

How to make rdirect in htaccess with character replacement in parameter?

There is a URL like:

/index.php?route=extension/payment/test/check&trx_id=321&lang_code=ru&merch_id=123&o.order_id=18&ts=20200413+10%3A38%3A31 You

need to check that route=extension/payment/test/check and do 301 redirect with the + symbol replaced by a T in the ts parameter.

The difficulty is that the set of parameters is not constant (they can be presented or not), and the replacement is needed only in ts.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Taran, 2020-04-14
@shambler81

GET - is not part of the url, and because of this RewriteRule it does not hit at all, from the word at all.
Therefore, it is distinguished through RewriteCond

# 301 --- http://www.test.com/faq.html?faq=13&layout=bob => http://www.test2.com/faqs.html
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{QUERY_STRING} (^|&)faq\=13($|&)
RewriteCond %{QUERY_STRING} (^|&)layout\=bob($|&)
RewriteRule ^faq\.html$ http://www.test2.com/faqs.html? [L,R=301]

And you can even change some of the parameters
by grouping them (...) into the cond and inserting them into the rule but not with $1 but %1
BUT the rule cannot change the get parameter, because it does not see it.
what it can do
It can remove all get parameters - It's not a typo at the end, the question mark removes parameters altogether. And in theory, now you can substitute, but not one, but all at once. You can also specify an explicit get parameter in the final url, BUT if you have all the incoming get parameters already specified, otherwise you will lose them. And that You can make a redirect with a get parameter to without a get You can do it from without a get to a get You can replace part of the get parameter
...faqs.html?
BUT the final output will be the sum of all get parameters plus your modified one, but this is a static construction except for your change, if 1 more get parameter appears in the url other than what you intended, it will be cut off when redirecting.
I hope it's clear?

D
dodo512, 2020-04-14
@dodo512

RewriteCond %{QUERY_STRING} (^|&)route=extension/payment/test/check($|&)
RewriteCond %{QUERY_STRING} ^(.*&)?(ts=\d+)\+(.*)
RewriteRule ^index\.php$ /$0?%1%2T%3 [R=301,L,NE]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question