How to insert/remove cookie to both www and non www part of the domain?

Lately my website switched from http and https, and I cannot make www to non www to work anymore. It now has a problem with user login, as if you log into the website with www, you stay logged out with non www, and vice versa. This creates some problems and confusion for users, so I decided to insert/remove cookies to both www and non www part of the domain upon logging in/out to mitigate this problem. I thought the solution from stack overflow will work, but today I got complaints from users again. Seems it doesnt…

So what is the right way to insert/remove cookie at both www and non www part of my website? Is this impossible to achieve? Or is it a problem if I use https?

I would not fix this problem, but rather the www to non www redirect, as the problem you’re currently trying to solve is like chasing your own tail, as you’re finding out.

What server are you using to serve content? Apache? Nginx? What is the configuration for redirects so far? What did you try to get www to non-www to work? etc

I am using apache, the issue is that my .htaccess is far more complex. It redirects all normal PHP requests to index.php, and all ajax server request to ajax.php. I can get the www to non www to work by trying out the solutions found on stackoverflow, but then it breaks all ajax script. Below is the .htaccess file on my server:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://mydomain.com/$1 [R,L] 
RewriteRule ^/?ajax/(.*)$   ajax.php?$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
#RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !\.(js|css|gif|jpg|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php [QSA,L]

As you can see we had www to non www redirection code there but it was commented out, since it the redirection to ajax.php would fail. This is why I decided to try something different, like inserting/removing cookies to both www and non www sites. I’ve already given up on trying to make www to non www redirection work with the other redirections(index and ajax), unless someone can find a magical solution.

The order in which you define these rules matters. The place where the commented code is now is halfway between some other rule, thereby breaking the existing rule. Try this instead:

RewriteEngine On

# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://mydomain.com/$1 [R,L]

# Rewrite www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

# Rewrite AJAX requests
RewriteRule ^/?ajax/(.*)$   ajax.php?$1 [NC,QSA,L]

# Rewrite anything else to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(js|css|gif|jpg|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php [QSA,L]

I tried your solution and it worked perfectly. Thank you, I guess I just dont know much about .htaccess, I am very grateful for your help.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.