I want to redirect all traffic on my webpage from www to non-www. So every request that goes to www.example.com
will be redirected to example.com
.
This is done in my .htaccess (taken from this question)
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
The problem is, that I previously didn’t have this rule in my .htaccess file. So if a user previously accessed the website using www.example.com
now the cache seems to prevent the redirect to example.com
and instead the www URL stays in the address bar.
If I however open a private browser window or clear the website data, the redirect works as expected.
I’ve tested it in both Chrome (88.0.4324.192) and Firefox (86.0), both browsers show the same behavior. OS: macOS 10.15.7
How can I fix this from a server-side perspective? Since I can’t tell all users to clear their cache.
Steps to reproduce:
- Clear cache and history in browser to start with a clean session
- Open
www.example.com
- Add rewrite rule www to non-www in .htaccess file
- Open
www.example.com
again (browser should have this address in his history from the last access). No rewrite toexample.com
will happen.
EDIT:
Maybe this occurs, because the browser has content already cached for www.example.com
and thus doesn’t even request the server. However the problem remains the same.
2
Answers
If their computers have already cached the website, it will be really difficult. in fact impossible to force a refresh since am assuming you did not put that in your code.
What you could do now is include a rule in your code that will force a periodic cache clearing. This way, your future updates will not face this same issue.
Or you could also completely disable caching which is not exactly ideal but it’s up to you and the design you have
–in HTML
–in .htaccess
Find other options for other languages from this link;
Another overkill solution is;
If there is anything on your website that does a database check and the result loads a new page, for instance a user session check or login; you could take advantage of that by modifying their database records in such a way that they will be forced to the login page. which will force them to reload when they get there and when they are forced to reload, their data is restored.
It’s a last resort but if the world depended on it sure, else. relax with the first solution. people will not keep the cache forever.
UPDATE
As I said in my previous response, you could set periodic caching in your next release. This can be simply done in HTML.
The differences between the 2 codes above are the
public
andprivate
which indicates whether anyone (public
) or just the end user (private
) can cache up to themax-age
(time in seconds).But this can also be done explicitly in several other languages. Here is an example in PHP
Here are some links that could assist you have a better understanding of how that works.
In place of using 301 response code, you can consider using 302 or 307, which is for Temporary redirect. Link
The response is not cached by default, unless indicated.