Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force non-www and https via htaccess

Tags:

I'm trying to force a user to be redirected to the non-www website, and, force https.

I've got this which sort of work, but doesn't force https, when http is entered.

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://site.com\.net/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^www\. RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] 

Any ideas on what I'm doing wrong?

like image 964
bear Avatar asked Jun 14 '11 16:06

bear


People also ask

How do I force HTTPS on all incoming traffic?

You can activate the feature to force HTTPS on all incoming traffic by following these steps: Go to File Manager in your hosting panel and open .htaccess inside the public_html folder. If you can’t locate it, make sure to create or unhide it. Save the changes. IMPORTANT: Make sure that the line RewriteEngine On is not repeated twice.

Why can’t I Find my htaccess file?

If it happens you couldn’t find the .htaccess file, chances are that it is hidden. To display the hidden file, click on the Settings button at the top right corner of the screen. A dialog box will pop open as soon as you click the button. In the box, check Show hidden dotfiles and click Save. You should now see the .htaccess file.

What is htaccess doing with my website?

Here is what it's doing: When placed in the root .htaccess, this technique covers all requests, providing complete https/non-www canonicalization for your site. Remember to replace the two instances of example.com with your own domain name.

How to force HTTPS connection with Hostinger?

After installing an SSL certificate, your website is available over HTTP and HTTPS. However, it’s better to use only the latter because it encrypts and secures your website’s data. While Hostinger allows users to configure this setting in just one click, you can also use the .htaccess file to force HTTPS connection. This tutorial will show you how.


1 Answers

Try this rule:

RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(www\.)?(.+) RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L] 
like image 112
Gumbo Avatar answered Sep 30 '22 16:09

Gumbo