Redirection-Rewriting changing just the domain and part of the string in the URL

Hi everybody.

I have a dynamically generated url like this:

https://www.domainA.com/payment.php?token=2571b646591359d3......

I need a redirection via htaccess to

https://www.domainB.com/payment.php?token=2571b646591359d3......

The part of the string to change is “domainA.com/payment” to “domainB.com/payment” because:

  1. Other parts of the website in the same domain should not be affected by the redirection.

  2. The rest of the URL after “token” changes for every user.

I have been trying to get a solution for hours before quitting.

I really appreciate some help.

Regards and thanks.

Options -MultiViews

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domainA\.com$ [NC]
RewriteRule ^/payment\.php$ https://www.domainB.com/payment.php [L,QSA]

Thanks very much for the answer, but I forgot to tell you that the first url:

https://www.domainA.com/payment.php?token=2571b646591359d3

triggers a 404-error, so your code does not work.

I need to redirect to the second URL after the 404 error.

Doesn’t matter what the response would be, redirects happen before any actual handling of the request. So the code should work. Or at least, if it doesn’t work it’s not because of the 404.

I cannot use Options -MultiViews in my website. It triggers a 404 error in all pages. I removed the page suffixes by htaccess.

Ah then you can probably not rewrite php files. You can try without Options -MultiViews, but I don’t think it’ll work.

I have a rewrite to change http to https:

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

All combined you would get the following:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.domainA\.com$ [NC]
RewriteRule ^/payment\.php$ https://www.domainB.com/payment.php [L,QSA]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.