Reputation: 1447
We have a webshop at, lets say, mywebshop.com
. For users from a specific country, lets say Germany, we have the domain mywebshop.de
, which redirects with a 301 to mywebshop.com
.
How can I let my .htaccess file set a cookie just before the redirect if the user navigates to mywebshop.de
so I know at mywebshop.com
it is a German customer? The cookie only needs to be set when navigating to mywebshop.de
, not when navigating to mywebshop.com
.
Eventually, some sort of paramater is also ok, but I don't know if the requested url has already some query params or not.
Upvotes: 3
Views: 424
Reputation: 785276
Put this code in your DOCUMENT_ROOT/.htaccess
file to set the cookie and do the redirect:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mywebshop\.de$ [NC]
RewriteRule ^ http://mywebshop.com%{REQUEST_URI} [R=301,L,NE,CO=REDIR_DE:1:%{HTTP_HOST}]
If you want add a query parameter while redirecting then use:
RewriteCond %{HTTP_HOST} ^(www\.)?mywebshop\.de$ [NC]
RewriteRule ^ http://mywebshop.com%{REQUEST_URI}?redir_de=1 [R=301,L,NE]
Upvotes: 2