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?
Backtracking...
How your question was initially written seemed to make more sense. Along the lines of...
- 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.
- 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?!)
TestOtherDomain1
is requested without any parameters?