How to get Cloudflare to work on non-www URLs?

Status
Not open for further replies.

cardboa7

New Member
Messages
12
Reaction score
0
Points
1
Cloudflare works when I navigate to www.mysite.x10host.com but it does not work when I navigate to mysite.x10host.com.

In other words, Cloudflare only works when "www." is in front. I know this because my browser says "this website is not secure."

Can anyone help me with this? How can I either get Cloudflare to work on non-www URLS, or how can I redirect all non-www traffic to their www equivalent?

Found a similar thread here but there is no clear answer. And this answer did not work.


Many thanks!
 
Last edited:

lylex10h

Active Member
Messages
982
Reaction score
71
Points
28
AFAIK in order to use CloudFlare, you need to use your own domain as CloudFlare requires using their name servers. Besides a CNAME, I'm really curious on how you are doing this. What is your www.mysite.x10host.com
 

cardboa7

New Member
Messages
12
Reaction score
0
Points
1
AFAIK in order to use CloudFlare, you need to use your own domain as CloudFlare requires using their name servers. Besides a CNAME, I'm really curious on how you are doing this.


@lylex10h - I simply enabled CloudFlare in cPanel.





My site contains sensitive information and is password-protected (hence why I want the HTTPS in the first place), so that's why I didn't want to publicly share the domain. Do you need to visit my website to help me?

Thanks
 
Last edited:

cardboa7

New Member
Messages
12
Reaction score
0
Points
1
Okay, I've figured it out.

Before you begin, ensure that hidden files are visible in cPanel File Manager.






First, I had to create a new .htaccess file in my public_html directory. (If the .htaccess file is not in this exact location, then the redirect code will NOT work.)

I then put the following redirect code inside of this .htaccess file:

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


I copied the above code verbatim from this Stack Overflow answer:








My website is password-protected with Basic Authentication via another .htaccess file that is located in my root folder (i.e.: /home/username).

The above redirect code only worked properly when this Basic Authentication code was removed. That is, if the Basic Authentication code existed (and my website was password-protected), my website would redirect to https://www.mysite.x10host.com/401.shtml, every time.

To fix this issue, I had to add the following two lines of code to the .htaccess file that is located in my root folder:

Code:
ErrorDocument 401 "Unauthorized Access"
RewriteEngine off


I found this solution on this blog post:


The blog post explains the Apache bug:
For basic authentication, the server writes a "401 Unauthorized" header and then looks for an error document based on a pre-defined path. Most of the time, the error document won't exist in the directory that you want to protect, so the request gets handled by the rewrite engine which throws a 404 error.

The solution to this problem is pretty straightforward. You need to add a single line of code to your .htaccess file instructing Apache to ignore the error document.










At this point, I thought that I was home free. My website would automatically convert every non-www-URL to the www-URL, just as desired. I could log-in to my website via Basic Authentication, without ever being redirected to 401.shtml.

Alas, I noticed a new problem...

When I would navigate to mysite.x10host.com with a cleared cache, the Basic Authenication dialog would appear, but the domain in the address-bar would still read http://mysite.x10host.com.

As stated in my last post, the primary reason why I wanted SSL in the first place was for this username/password form. But, my website would convert the non-www domain to the www-domain only after logging into the site. So, the login was still unsecure.

To fix this issue, I had to wrap my Basic Authenication code (found in .htacess) with an Apache If directive, like so:

Code:
<If "%{HTTP_HOST} == 'www.mysite.x10host.com'">
AuthType Basic
AuthName "Password Protected"
AuthUserFile /path/to/htpasswd
require valid-user
</If>


What this If directive does is make sure that the user is only prompted for the password AFTER the domain has been redirected to the www version.

I learned about this method from:



At last, my website is finally working as desired, without a hitch.

I hope that this post finds other lost souls.
 
Last edited:
Status
Not open for further replies.
Top