-1

I've only just realised that when I go to https://www.mywebsite.com and then https://mywebsite.com the cookies haven't carried over from my login. Does anyone know how I set it so that all traffic to https://mywebsite.com is redirected to https://www.mywebsite.com on namecheap?

willmahon
  • 319
  • 4
  • 13

1 Answers1

1

In gneral, you cannot have the same cookies work for two different domains unless if the domain is explicitly named in the Set-Cookie header. See this answer. (Yes! https://mywebsite.com and https://www.mywebsite.com are two different domains). You will have to do a redirect from https://mywebsite.com to https://www.mywebsite.com using .htaccess file placed in your websites's root directory.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

You can refer to NameCheap documentation on how to redirect the URL.

Ravi Kumar CH
  • 461
  • 1
  • 6
  • 16
  • Is there a way to do this in namecheap? – willmahon Aug 27 '21 at 01:56
  • Yes, i have updated the answer with the link to the namecheap documentation on where to do the redirect. – Ravi Kumar CH Aug 27 '21 at 02:02
  • This answer is not correct regarding cookies. You can share cookies between a domain and subdomains using **set-cookie: name=value; domain=example.com**. https://stackoverflow.com/a/23086139/8016720 – John Hanley Aug 27 '21 at 02:53
  • @JohnHanley, i have updated the answer to be more clear. Thank you for pointing it out. – Ravi Kumar CH Aug 27 '21 at 03:19
  • @RaviKumarCH - I recommend expanding your answer. First, redirects are good. However, implement two redirects: a) from HTTP to HTTPS; b) from example.com to www.example.com (or the reverse). 2) Provide more details on correct cookie handling for domains and subdomains. 3) Correctly designed sites implement both. Your answer only partially covers best practices. As written, you will confuse more than help. – John Hanley Aug 27 '21 at 05:57
  • So for a react app to I just create a .htaccess file with this code and put it in the public directory? – willmahon Aug 27 '21 at 13:17