简体   繁体   中英

Add parameter using htaccess on condition

This will be a simple for those familiar with Apache rules.

Situation

Using Alipay for a payment platform, the return URL cannot feature any of your own URL parameters (be it GET or POST). However, I am using Joomla and specifically Akeeba subscriptions. This component expects a parameter in the URL in accordance with the payment platform in question.

I want to detect (through one of Alipay's URL parameters) when a return page is hit and add the extra parameter.

Example (domain and page redacted)

http://...?
    currency=HKD&
    total_fee=2.00&
    out_trade_no=211&
    trade_no=2014040100276615&
    trade_status=TRADE_FINISHED

Desired outcome

http://...?
    currency=HKD&
    total_fee=2.00&
    out_trade_no=211&
    trade_no=2014040100276615&
    trade_status=TRADE_FINISHED&
    paymentmethod=alipay

The simple addition of a &paymentmethod=alipay

Problem

I can't seem to get Apache to pick up the rule; here are a couple of attempts so far. Please note, I definitely can use .htaccess and don't need to change RewriteBase.

-- Attempt 1 --
RewriteCond %{QUERY_STRING} out_trade_no=
RewriteRule ^out_trade_no paymentmethod=alipay&out_trade_no [R,L,QSA]

-- Attempt 2 --
RewriteCond %{QUERY_STRING} (^|&)out_trade_no=(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}&paymentmethod=alipay [L,R=301,QSA]

Progress

Combining the two, I have made progress but, now seem to have the Rewrite part spamming "paymentmethod=alipay" which seems to cause an error.

RewriteCond %{QUERY_STRING} out_trade_no=
RewriteCond %{QUERY_STRING} !paymentmethod=
RewriteRule ^ %{REQUEST_URI}&paymentmethod=alipay [R,L]

Now getting a redirect chain until it automatically stops at a redirect limit

If you are just trying to match a query string from that URL with that rewritecond you need to match the first one( currency ). Which is the easiest.

Try this. It will send all the parameters you want.

RewriteCond %{QUERY_STRING} ^\bcurrency=
RewriteRule ^(.*)$ /$1?paymentmethod=alipay [R,QSA,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM