Is it possible to check a specific domain request with HTACCESS and add some query string?

Actually my entire task wants to pass some cookie values as URL parameters if a specific domain is requested.


  • IF requested OtherDomain1.example
    • IF cookie named TestCookie1 is set (a browser cookie created with help of Javascript)
      • add TestCookie1 value to request URL OtherDomain1.example as parameters (OtherDomain1.example?q=TestCookie1-value)

so every request to OtherDomain1.example from my Apache server is rewritten by adding some URL parameters after reading the browser cookie.

If cookie is not set or cookie value is empty then nothing will happen the requested domain will work as usual no change.... if there any limitations to set else I am ok with passing empty query strings...

And if the domain is requesting with a URL-Path then that also needed to transfer...

  • 1
    What should happen if the cookie does not exist (or does not have a value)? Do nothing? Or still redirect but with an empty value? What about the URL-path? Only the document root or any URL-path? Does the URL-path also need to be transferred?
    – MrWhite
    Commented Mar 14, 2020 at 14:34
  • What is TestOtherDomain1 is requested without any parameters? Commented Mar 14, 2020 at 17:14
  • @MrWhite the URL-path also needs to transfer just like normal but if the cookie exist just bring it that too... And if the Cookie is empty or not exist or no value then nothing will happen the request will work as usual, and if there is any limitation I am ok with passing empty parameters if no cookies... Commented Mar 14, 2020 at 18:01
  • @StephenOstermiller sorry got some mistake in the example domain, Question just updated. OtherDomain1.example is the external site domain and for my case, my URLs to OtherDomain1.example are currently not passing any parameters all hyperlinks just passing with URL-Paths... But I think it would be good if there is an option to check and do needful append... Commented Mar 14, 2020 at 18:23
  • So, (from your updated question), there is only 1 domain here - you are redirecting back to itself?! "OtherDomain" is really just this domain? Although your comment above does imply 2 domains? ... "OtherDomain1.example is the external site domain"? Is "OtherDomain1.com" under your control?
    – MrWhite
    Commented Mar 14, 2020 at 19:42

1 Answer 1


MyDomain1.example is my current domain and OtherDomain1.example is one of an external domain so Our Server that belongs to MyDomain1.example check if there is any request to OtherDomain1.example and do remaining...

I still think I'm missing something here? It sounds like you are wanting to intercept and modify the request coming from your site MyDomain1.example (or rather, the client's browser when looking at your site - I assume) before it reaches OtherDomain1.example, which is presumably a domain out of your control?

This isn't something you can do in .htaccess. Using .htaccess, you can only intercept the request at the destination, ie. OtherDomain1.example - but the cookie presumably isn't set for OtherDomain1.example?

This sounds like a task for JavaScript - to modify the outbound anchors in the HTML source before (or "as") the user clicks on them?

Or, is the scenario different to this?


How your question was initially written seemed to make more sense. Along the lines of...

  1. User sends request to MyDomain1.example, for which a cookie (eg. TestCookie1) has previously been set. Consequently, the user's browser sends the cookie as part of the request.
  2. You wish to redirect the users request to OtherDomain1.example. You need to also forward the value of the cookie sent on the request to MyDomain1.example as a URL parameter (since the user does not have a cookie set for OtherDomain1.example).

This could be done with something like:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^mydomain1\.example
RewriteCond %{HTTP_COOKIE} (?:\bTestCookie1=([^;]*))?
RewriteRule (.*) https://otherdomain1.example/$1?q=%1 [QSA,R,L]

The condition that checks against the HTTP_HOST server variable is not required if this is the only domain hosted at this server.

The condition that checks against the HTTP_COOKIE server variable (the HTTP Cookie header as sent by the browser) either matches TestCookie1=<anything> anywhere in the cookie header or "nothing" at all - either is considered successful. The cookie value is captured and this is passed through to the substitution string in the %1 backreference. the original URL-path on the request is passed through to the target URL in the $1 backreference. The QSA flag allows any additional query string parameters on the initial request to be passed through also.

However, this redirects every request sent to MyDomain1.com to OtherDomain1.com.

(Although, this may be answering a question you didn't even ask?!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.