Redirecting www to non www

I agree, if he can edit .htaccess he’d better save the page rules.
I used the page rules because, for that website, I couldn’t edit .htaccess.

1 Like

Yes, it was. I added $1 to the forwarding URL, as explained in the article. Now it’s redirecting :slight_smile:

Now the problem is it’s redirecting all www URLs to home page.

Yes using WordPress for blog (in subfolder), but can’t access server (.htaccess). The main site is in Asp.Net.

Can you please tell the rules required. Thanks.

1 Like

Well, mine is working now with only one page rule :slight_smile: Maybe it needed some time, I purged the cache also.
Thanks!

1 Like

Glad you got it sorted. Page Rules are valuable, so good you were able to get it working with just 1.

On that subject, since you are running Wordpress, it’s advised to create a Page Rule for wp-admin area to bypass cache.

This video is pretty helpful (wish I had found it earlier) and covers the wp-admin page rule ~3:30 mark (along with some other tips):

2 Likes

Yes Indeed it’s very helpful. One thing not able to get is when we have the option of “Always use HTTPS” under crypto then why we need to use one page rule to always use https. I am not using any page rule for this. Am I missing something?

Forwarding URL - www.mydomain.com/* to https://mydomain.com/$1

There’s no need for a page rule now that the Always Use HTTPS function is available. The video is just a little outdated.

1 Like

Unfortunately that is the case. Always use HTTPS was a very popular page rule that we wanted to make sure people were aware of (so we mention it a lot). We were eventually able to build it into the interface to make it even easier, but now are still going back and cleaning up the more confusing references.

1 Like

@ryan could you please confirm that the syntax used in a message up here, mydomain.com/$
($ without a number) is correct? I have read all the examples in the documentation, and they only use $1, $2, etc.

Wow, I didn’t know that Always Use HTTPS had been added to the interface. Great way to save at least a page rule… thank you for the tip!

There needs to be a number associated with the $ or else it won’t match/replace correctly.

As expected. Thank you for the confirmation.

1 Like

Typo mistake, it’s $1. Corrected.

Hi Andy, bothering you again. Are you absolutely sure this is required? While reading more about CNAME, found that the purpose is different.

Isn’t the forwarding page rule enough for the redirection? In my case, maybe it was just cache purge required and after some time it start working.

Hey, it’s no bother :smiley:

Well… I’m not absolutely sure - I can only tell you my results from testing.

I have a test site set up with the following forwarding rule:
*www.4ndy.us/* --> https://4ndy.us/$1

I deleted the www CNAME record. If I try and go to www.4ndy.us my browsers give me an error (you can see for yourself). When the www CNAME record is present the forwarding rule works fine.

Based on my understanding, without the www CNAME present DNS will not resolve.

2 Likes

Could you help me, I’ve done everything and my domain does not work without “www” see the attachments, you look at me and see if they are correct?

Anyone get it working - if so. Can you please post what works for you instead of saying you have it working.

Simply put the following lines of code into your main, root .htaccess file.
In both cases, just change out domain.com to your own hostname.

Redirect to www

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
</IfModule>

view rawgistfile1.apacheconf hosted with by GitHub

Redirect to non-www

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
</IfModule>

{redacted}

Here is the page rule setup you need. Replace example.com with your domain.

Create a page rule with the “matching URL” set to:

www.example.com*

(The asterisk appearing after your domain is important)

Then, click “add a setting” pick “Forwarding URL”, and set the status code to 302 - temporary redirect.

In the “destination url” field, enter

https://example.com/$1

The $1 here is important for redirects to keep the page they were on - this makes it so www.example.com/about will redirect to example.com/about.

Your page rule should looks like this (example.com will be your domain):
image
Once that is done, you can hit “save and deploy”. Your website should start redirecting www to non-www within the next minute.


Redirect non-www to www

If you instead want to redirect non-www to www, follow the same steps as above, except, the “forwarding URL” should be your non-www address, like so:

example.com*

And the “destination URL” should be your www version, like so:

www.example.com/$1

Again, the $1 is important to keep the page/URL the same when redirecting.

image

Hit “save and deploy” and your website should properly redirect the non-www version to the www version within the next minute.


If it’s not working

A requirement for either of these solutions to work, you MUST have a DNS record for BOTH the www and non-www versions of your website.

If you don’t have a www DNS record, go to the DNS tab in Cloudflare and add a “A” type DNS record. Set the “name” to www and set the IP address to either your server’s IP address, OR the IP “192.0.2.1”. Finally, make sure the cloud is orange :orange:, NOT grey :grey: and hit “add record”.

If you don’t have a DNS record for non-www (also known as the “apex” or “root”), go to the DNS tab in Cloudflare and add a “A” type DNS record. Set the “name” to an “@”/at sign and set the IP address to either your server’s IP address, OR the IP “192.0.2.1”. Finally, make sure the cloud is orange :orange:, NOT grey :grey: and hit “add record”.

4 Likes