Serving Static Content from a Cookieless Domain -

2019-08-09 04:49发布

问题:

I can have many images per page (e.g. 70) and want to serve them without the overhead of cookies.

I would prefer to set up a subdomain and add a CNAME record (as opposed to having to use a separate domain). Currently, I have a CNAME record for my donmain: www.example.com ==> example.com and I want to keep the functionality of resolving example.com to www.example.com.

I've discovered that when I access my site using www.example.com it uses different cookies from when I use example.com. Could I use .htaccess to redirect all example.com requests to www.example.com requests and solve the problem this way? Would all cookies then default to www.example.com when set?

UPDATE Then I would create the CNAME record static.example.com and point it to www.example.com. I would then point all my image requests to static.example.com. Would they be cookieless?

回答1:

Yes that's what is needed and good for SEO too. Multiple url for same content is not a good thing. You need to make changes in htaccess file written below.

RewriteBase / RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]



回答2:

You're on the right track, but it's not immediately guaranteed that static.example.com will be cookieless. You'll need to explicitly set all your cookies for www.example.com so that they won't be shared among subdomains. A separate domain is much easier to deal with in this regard.