Art
Art

Reputation: 848

.htaccess - need to replace "?" in URL with "&"

So, I have this url that unfortunately cannot be modified, and paypal checkout appends the token with "?token=.....". The problem is that the proxy url already contains several parameters, so the paypal token must be appended with "&token=.....", otherwise "token" is not visible as a query string var.

The only way think it might work is to replace the "?" with "&" in the .htaccess file. ( Unless someone has a better idea! )

URL including paypal token:
https://some.samplesite.com/proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success?token=EC-8FJ83689WF858702D

I need the .htaccess rule that would do the trick. I am also including the existing .htaccess file to make sure other rules do not get violated.

RewriteEngine on
RewriteBase /

# if not https, redirect
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# if only / is present, need to stop looping (domain.com/)
RewriteRule ^$ - [L]

# match subdomain
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^app
RewriteCond %{HTTP_HOST} ^([^\.]+)\.[^\.]+\.[^\.]+$
RewriteRule ^(.*)$ /$1?sub=%1 [QSA]

# ignore any .php|.html|.asp|.ico files and certain directories
RewriteRule ^(exec|images|css|js|xyz|(.*)\.php|(.*)\.html|(.*)\.asp|(.*)\.ico)($|/) - [L]

# stop if index.php
RewriteRule ^index\.php$ - [L]

# map all requests to 'path'
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L]

Thanks for the help!

Edited: Ultimately the string will be broken into following query string params

*path* - proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/

*sub* - some

*trans* - success

Please note trans is actually a part of path, but b/c it was appended using "&", it is converted into a query string param.

htaccess log

init rewrite engine with /proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
strip matching prefix: /proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success -> proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
applying pattern ^ to uri proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
RewriteCond: input='443' pattern='!^443$' => not-matched
strip matching prefix: /proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success -> proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
applying pattern ^ to uri proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
RewriteCond: input='GET /proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success HTTP/1.1' pattern='\s/+(.+?)\?(token=[^&\s]+\S*)\s' => not-matched
strip matching prefix: /proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success -> proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
applying pattern ^$ to uri proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
strip matching prefix: /proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success -> proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
applying pattern ^(.*)$ to uri proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success
RewriteCond: input='' pattern='^$' => matched
RewriteCond: input='demo.samplesite.com' pattern='!^app' => matched
RewriteCond: input='demo.samplesite.com' pattern='^([^\.]+)\.[^\.]+\.[^\.]+$' => matched
Rewrite URL to >> /proxy/091F126B7B6D624623606A1BB67EDC430885ABAF02AD5975FF2A8276E21FD083091F126B7B6D624623606A1BB67EDC430151C3DF14A66603F9389B45EC89C9057C76A20EA0B4746BA97D7027E67E9EBC6437DCCA31B42521A4794F8E766AB37A76C0E6BD4FC92C60351E7C5FADB37CF0F82210A04BF29A74/&trans=success?sub=demo&token=EC-76D265115N209873K [REWRITE]

Upvotes: 1

Views: 1021

Answers (2)

anubhava
anubhava

Reputation: 785176

Have it this way:

RewriteEngine on
RewriteBase /

# if not https, redirect
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]

# convert ?token=... to &token=...
RewriteCond %{THE_REQUEST} \s/+(.+?)\?(token=[^&\s]+\S*)\s [NC]
RewriteRule ^ /%1&%2? [L,NE,R=302]

# if only / is present, need to stop looping (domain.com/)
RewriteRule ^(index\.php)?$ - [L,NC]

# match subdomain
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^app
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$
RewriteRule ^(.*)$ $1?sub=%1 [L,QSA]

# ignore any .php|.html|.asp|.ico files and certain directories
RewriteRule ^(exec|images|css|js|xyz|(.*)\.php|(.*)\.html|(.*)\.asp|(.*)\.ico)($|/) - [L]

# map all requests to 'path'
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1 [L,QSA]

Update:

While testing this with URL:

http://abc.dev.localhost/proxy/foobar/&trans=success?token=EC-8FJ83689WF858702D

It redirects to:

http://abc.dev.localhost/proxy/foobar/&trans=success&token=EC-8FJ83689WF858702D

And displays this for print_r($_GET) output:

Array
(
    [path] => proxy/foobar/
    [trans] => success
    [token] => EC-8FJ83689WF858702D
    [sub] => abc
)

Upvotes: 2

Jon Lin
Jon Lin

Reputation: 143886

Try adding this right before the # if only / is present, need to stop looping (domain.com/) comment:

RewriteCond %{QUERY_STRING} ^(.*)\?token=(.*)$
RewriteRule ^(.*)$ /$1?%1&token=%2 [L,R,NE]

Upvotes: 1

Related Questions