mdown mdown - 3 months ago 23
Apache Configuration Question

htaccess redirect with query string preserved

What I'd like to happen via a .htaccess redirect is:

An url request of : "http://subdomain1.mysite.com/addToBasket.php?query_string"

is redirected to

"http://subdomainA.subdomain1.mysite.com/addToBasket.php?query_string"

The subdomains 'subdomain1' and 'subdomainA.subdomain1' are properly set up in DNS as fully qualified domain names

However, subdomain1.mysite.com exists on a different server than subdomainA.subdomain1.mysite.com - so absolute url format is required throughout.

In the request site subdomain directory the 'addToBasket.php' script file will not exist but will exist in the redirected to subdomain directory

The requested query string is a wildcard but must not be altered during the redirection.

I've searched and tried many of the example solutions - but the '?query string' part is always being omitted in the redirection.

Reason for the subdomain1 => subdomainA.subdomain1 madness and differing servers is that a new site is being launched on the new server with a fresh start DB but the old site generated basket related emails to customers containing links which they clicked to fulfil the order process - so the old site will still be hosted on the old server for a while but under a subdomain just to allow time for these email links still in the wild to work with the system they were designed for.

Unfortunately the new developers created a similarly named subdomain system as used in the old site - so the old site will now have a 'subdomain.subdomain' url format to allow it to have a different IP and function.

Any help very much appreciated!

Answer

Unless you explicitly overwrite the query string, it will be preserved automatically, see RewriteRule

Modifying the Query String

By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

This means, if you just redirect without adding a query string yourself, it will be passed on, e.g.

RewriteRule ^addToBasket.php$ http://subdomainA.subdomain1.mysite.com/addToBasket.php [L]

or if you want to redirect not just this one, but every possible request

RewriteRule ^ http://subdomainA.subdomain1.mysite.com%{REQUEST_URI} [L]