Redirecting www to non-www using .htaccess file

Hello,

Can someone please help me with a coding problem I’m having redirecting www to non-www.

I’ve used Cloudflare’s free service for a padlock secured connection on my selling website (https://foragers.scot) which uses Rapidcart Pro.

Non www URL works fine however www.foragers.scot is not secure and needs to be redirected to https://foragers.scot. I have created an .htaccess file (using suggested code from https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www);

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

But this is taking me to https://foragers.scot/public_html/ with a 404 Not Found error.

The .htaccess file is located in the root directory and I have tried moving it to the public_html folder but this is not the solution.

I hope some kind person can see where I am going wrong.

Many thanks,
Gary

Not sure if I under stand what you’re saying.
But this should redirect sites from WWW to non-WWW (secured):

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

Many thanks Doug,
I’ve edited the .htaccess files as per your instruction;

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.foragers\.scot$
RewriteRule ^/?$ "http\:\/\/foragers\.scot\/" [R=301,L]

But www.foragers.scot still takes me to https://foragers.scot/public_html/. I need it to go to https://foragers.scot/ (without public_html/). See attached screen grabs.

All my website files reside in the public_html folder.

I think the .htaccess file needs a minor edit to correct this but as you will gather my coding skills are pretty basic so any suggetions would be most appreciated.

Best regards,
Gary

That’s not at all the code I gave you above.

The code I listed above has no URL’s as they aren’t needed, it also has different options to support the Query string.

If I paste this into my browser:

 www.foragers.scot

2019-07-12_12-21-03

I get this:

You have something else going on. RapidCart Pro is adding a query string to the end of the URL. That should work fine with the code I gave you but you’re not using the code above(it has been tested and works with query strings). I’m not seeing any redirects happening at all. No public_html like you show. No stripping of the www like what you wanted.

It could be that because you used 301 (permeant) with the old htaccess file they may very well be being cached. 301’s have no expiry date (why they are called permeant) so the can technically be cached forever.

The code you posted I just tested and doesn’t work to strip the www.

Might I suggest you double check the file placement and delete the .htaccess file you currently have. Copy and paste exactly what I have given you and try again. You might want to change the r=301 to r=302 until you are sure it works 100% of the time for every URL on the site. The change it back to the r=301.

Sorry I assumed I had to paste in my specific URL. After pasting in the correct code and clearing site data it works perfectly.
really appreciate your help.
Many thanks,
Gary