Home > OS >  htaccess redirect URL with parameter when a special parameter exists
htaccess redirect URL with parameter when a special parameter exists

Time:09-23

I hope to get help for the following task:

I want to redirect a domain with htaccess only when index.php is called and when a special parameter is given. Let's say I have the following URL

https://www.my-domain.com/index.php?action=status-check&licence=80586f63&product=RFDE&domain=domain.tld

When this URL is called and the parameter action exists it should be redirected to

https://www.other-domain.com/index.php?action=status-check&licence=80586f63&product=RFDE&domain=domain.tld

If just

https://www.my-domain.com/index.php?parm=4711

or

https://www.my-domain.com/index.php

is called or any other URL with in this domain but without the parameter action, nothing should happen.

Can somebody help me to find the right condition and rule?!

Thank you!

CodePudding user response:

You could do something like the following at the top of your root .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)my-domain\.com
RewriteCond %{QUERY_STRING} (^|&)action=
RewriteRule ^index\.php$ https://www.other-domain.com/$0 [R=302,L]

This redirects www.my-domain.com/index.php (with or without the www subdomain) when the action URL parameter exists anywhere in the query string (although your example only shows this at the start of the query string?). The URL-path and query string are both preserved in the redirect to www.other-domain.com. The action URL parameter could have any value.

The $0 backreference contains the entire match of the RewriteRule pattern, ie. index.php on success. The original query string is passed through by default - there is nothing extra you need to do here in order to redirect to the same query string.

If www.other-domain.com is on a different server and no other domains could conflict at the originating host then you could perhaps remove the first condition that checks against the HTTP_HOST server variable.

  • Related