I already tried couple solution from Google, but some of them didn't work (at all), others resulted in weird bug, where HTML page was loaded, but assets (CSS and images) didn't. Other times there were too many redirects and page freaked out. I have account on Cloud Flare, but both HTTP and HTTPS sites are accessible and changing setting that should redirect users to HTTPS, doesn't work.

So my question is how can I use .htaccess to redirect from (example)

http://www.daniweb.com and http://daniweb.com
to
https://www.daniweb.com

To sum it up, if not HTTP, move to HTTPS, and to subdomain www.
"If not starts with https://www move to https://www". Except in "htaccess"-language.

Recommended Answers

All 5 Replies

Ooooh, we used to do this, but then we switched from Apache to Nginx, and Nginx doesn't use .htaccess files. I think we did something along these lines:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^daniweb.com [NC]
RewriteRule ^(.*)$ https://www.daniweb.com/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

I wish I could just pull up the exact rules I had. :( Hope this helps.

commented: Thanks! +4

Unfortunately both of these are also found in Google. The first didn't affect anything, the second made website redirect unto itself back in the day.

I'll try it anyways, maybe it's different, I don't know, it still looks cryptic to me xD

(couple seconds later), yep, those work exactly same: "The www.---.com page isn’t working

www.---.com redirected you too many times."

Nevermind, it looked like 6th and 7th line did something that caused redirection. Now it works. Thanks :)

Finally, no worries.

What did you change to get it to work?

Removed two last lines. Apparently there was something to it that made me redirect trillion times. As soon as I removed it, everything from http://www. and http:// got redirected to full https://www..

-

If you're from search engine, this is what you seek for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

I don't know what L, is, or what [NC] is, but it works and hasn't exploded.

I see how that will redirect the non-www version to the SSL www version. However, i don't see how it will redirect to the SSL version when using www already.

Also, NC means not case sensitive. And L means to stop processing any additional rules in the .htaccess file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.