Forum Moderators: phranque

Message Too Old, No Replies

Correcting double question marks with .htaccess

         

tcvdveer

1:09 pm on Nov 29, 2013 (gmt 0)

10+ Year Member



I am trying a way to correct a response URL with double questionmarks,

Example:
http://www.example.com/index.php?dispatch=payment?ref=123


Should become:
http://www.example.com/index.php?dispatch=payment&ref=123


Tried:
RewriteCond %{QUERY_STRING} ^dispatch=payment\?ref=(.*)$
RewriteRule ^(.*)$ $1dispatch=payment&ref=%1 [QSA]

But it displays wrong as:
http://www.example.com/index.phpdispatch=payment&ref=123

[edited by: incrediBILL at 9:53 pm (utc) on Dec 7, 2013]
[edit reason] Please use EXAMPLE.COM for all domain names [/edit]

aakk9999

2:18 pm on Nov 29, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^(.*)$ $1dispatch=payment&ref=%1 [QSA]


I don't think you want this. You should 301 redirect to the correct URL instead.

tcvdveer

3:28 pm on Nov 29, 2013 (gmt 0)

10+ Year Member



Thanks, that seems to solve it!

lucy24

9:28 pm on Nov 29, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^(.*)$ $1dispatch=payment&ref=%1 [QSA]

But it displays wrong as:
http://www.example.com/index.phpdispatch=payment&ref=123

Chorus:
It never does quite what I want
But only what I tell it


#1 The QSA flag is used only when you need to add to the existing query string while keeping any parts that were already there. Here you're trying to change or replace part of the query, so the appropriate flag is [R=301,L].

#2 The capture expressed as $1 is only the "path" of the request. To make the new stuff into a query, you have to put a ? between path and query.

The rule as written will only work if the elements of the query string always come in the same order.

There's a reason for the insistence on example.com.

g1smd

9:17 pm on Dec 6, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks, that seems to solve it!
Post the new code. There's plenty of scope for writing code that appears to work but still has problems.

tcvdveer

11:21 pm on Dec 6, 2013 (gmt 0)

10+ Year Member



For applying a successful solution I adjusted the htacces to:

RewriteCond %{QUERY_STRING} ^dispatch=payment\?ref=(.*)$
RewriteRule ^(.*)$ $1dispatch=payment&ref=%1 [R=301]

lucy24

2:41 am on Dec 7, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Without a question mark in the target, AND no [L] flag?

Incidentally you don't need anchors in the (.*) pattern. By default, a Regular Expression starts as soon as it can, and continues for as long as it can. Anchors are only needed when you're looking for specific content in a specific location.

g1smd

7:17 am on Dec 7, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The rule target is missing protocol, hostname and a question mark.

The rule is missing the [L] flag.

tcvdveer

10:50 am on Dec 7, 2013 (gmt 0)

10+ Year Member



Adjusted to (and confirmed working):

RewriteCond %{QUERY_STRING} ^dispatch=payment\?ref=(.*)$
RewriteRule ^(.*)$ $1dispatch=payment&ref=%1 [R=301,L]


The actual usage is to correct following return URL from the bank:
http://www.example.com/cscart4/index.php?dispatch=payment_notification.fail&payment=eurobnk?ref=12_reff45b176bc


to:
http://www.example.com/cscart4/index.php?dispatch=payment_notification.fail&payment=eurobnk&ref=12_reff45b176bc


Using:

RewriteEngine on
RewriteBase /cscart4
RewriteCond %{QUERY_STRING} ^dispatch=payment_notification.fail&payment=eurobnk\?ref=(.*)$
RewriteRule ^(.*)$ $1\?dispatch=payment_notification.fail&payment=eurobnk&ref=%1 [R=301,L]

[edited by: engine at 12:25 pm (utc) on Dec 7, 2013]
[edit reason] please use example.com [/edit]

phranque

1:39 pm on Dec 7, 2013 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



RewriteRule ^(.*)$ $1\?dispatch=payment_notification.fail&payment=eurobnk&ref=%1 [R=301,L]

the rule target is still missing the protocol and hostname and you don't need the backslash before the question mark.

g1smd

2:19 pm on Dec 7, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the RegEx pattern in the Condition, the literal period needs a backslash.

lucy24

10:28 pm on Dec 7, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteBase /cscart4

So the target is really
http://www.example.com/cscart4/$1?dispatch=payment_notification.fail&payment=eurobnk&ref=%1
?

Is this htaccess file located in the /cscart4/ directory?