# Redirecting www to non-www using .htaccess file

**URL:** <https://forums.realmacsoftware.com/t/redirecting-www-to-non-www-using-htaccess-file/27801>\
**Category:** Lounge\
**Created:** [July 12, 2019, 5:12pm UTC](https://forums.realmacsoftware.com/t/redirecting-www-to-non-www-using-htaccess-file/27801 "2019-07-12T17:12:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garymac](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/garymac/32/1198_2.png) [@Garymac](https://forums.realmacsoftware.com/u/Garymac)\
**Post date:** [July 12, 2019, 5:12pm UTC](https://forums.realmacsoftware.com/t/redirecting-www-to-non-www-using-htaccess-file/27801/1 "2019-07-12T17:12:47Z")

</div>

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](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](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](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/](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

---

<div class="post-metadata">

**Author:** ![teefers](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/teefers/32/8173_2.png) [@teefers](https://forums.realmacsoftware.com/u/teefers)\
**Post date:** [July 12, 2019, 6:00pm UTC](https://forums.realmacsoftware.com/t/redirecting-www-to-non-www-using-htaccess-file/27801/2 "2019-07-12T18:00:56Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Garymac](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/garymac/32/1198_2.png) [@Garymac](https://forums.realmacsoftware.com/u/Garymac)\
**Post date:** [July 12, 2019, 7:11pm UTC](https://forums.realmacsoftware.com/t/redirecting-www-to-non-www-using-htaccess-file/27801/3 "2019-07-12T19:11:54Z")

</div>

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/](https://foragers.scot/public_html/). I need it to go to [https://foragers.scot/](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

 ![41](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/2X/e/e7672d818264f22d2d09e2715574b7a13c26b05c.png) ![44](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/2X/8/8b6890fb686e1942317d20498316c1360703a57b.png)

---

<div class="post-metadata">

**Author:** ![teefers](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/teefers/32/8173_2.png) [@teefers](https://forums.realmacsoftware.com/u/teefers)\
**Post date:** [July 12, 2019, 7:43pm UTC](https://forums.realmacsoftware.com/t/redirecting-www-to-non-www-using-htaccess-file/27801/4 "2019-07-12T19:43:29Z")

</div>

> [@Garymac](#):
>
> RewriteEngine on RewriteCond %{HTTP\_HOST} ^www.foragers.scot$ RewriteRule ^/?$ “http://foragers.scot/” [R=301,L]

### 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.

> [@Garymac](#):
>
> www.foragers.scot

If I paste this into my browser:

```auto
 www.foragers.scot

```

![2019-07-12_12-21-03](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/2X/8/8ee695e5ac74a88c244042c2914af69431096499.jpeg)

I get this:

 ![2019-07-12_12-19-28](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/2X/c/c485cc38d8823c02a497a059c4004006c6f678f4.jpeg)

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`.

---

<div class="post-metadata">

**Author:** ![Garymac](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/garymac/32/1198_2.png) [@Garymac](https://forums.realmacsoftware.com/u/Garymac)\
**Post date:** [July 12, 2019, 8:12pm UTC](https://forums.realmacsoftware.com/t/redirecting-www-to-non-www-using-htaccess-file/27801/5 "2019-07-12T20:12:36Z")

</div>

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
