Why is "www.example.com" not the same as "example.com"?

I have a website. If I log in to the domain of this format http://example.com , and then change my address to http://www.example.com , I found that my account was not logged in. If I change the address to http://example.com , I find that my account is registered.

I contacted my host, they told me about a programming problem.

How can I solve this problem so that both addresses represent the same access / session / cookies?

I use PHP and MySQL

+6
source share
3 answers

www.example.com and example.com are two different domains, as far as the browser is concerned, apparently, although they are both directed to the same site. The same thing happens if you park another domain there, say example.net .

To solve the problem, quite often rewrite the URL through .htaccess. Determine which domain name you prefer to use, and add something like this to your .htaccess:

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

or

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

(the first removes, the second adds www)

+9
source

How can I solve this problem so that both addresses represent the same Access / session / cookies?

You must set the domain path of your cookie like this to make it accessible to all subdomains: (www is a subdomain):

.domain.com

+6
source

This is not the same .. usually you can go to www.example.com just by writing example.com in your browser, but your browser has added www to your url .. so basically it isn’t.

-1
source

Source: https://habr.com/ru/post/901190/


All Articles