Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache redirect http to https and www to non www

Tags:

basically what i want is redirect al request to use HTTPS instead of http

I have this in my htaccess so far and it worked great: Code:

<IfModule mod_rewrite.c>  RewriteEngine On  RewriteCond %{HTTPS} off  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}  </ifModule>  

today someone noticed that when going to: http://www.example.com it redirects to and shows an unsecure connection thingie.

My ssl is setup for non www domain: mydomain.com

So i need to make sure all site requests are sent to non www and https: It works fine if i put example.com it redirects to https://example.com

but with www.example.com it goes to htts://www.example.com and shows the error

what do i need to add to my code to redirect www to non www and then to ssl ?

like image 634
misulicus Avatar asked Mar 30 '12 15:03

misulicus


2 Answers

You will have to re-issue your certificate for both www and without www.

If someone connects to your site via a domain name that is not included in your common name, they will receive a warning.

The ssl negociation process happens before any response from the server (in your case, a redirection), so in all cases, your visitors will receive a warning when using a domain that is not in your common name.

like image 74
Pierre-Olivier Avatar answered Sep 22 '22 05:09

Pierre-Olivier


You can get what you need from the HTTP_HOST

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

This way it will get the host always without the subdomain.

like image 21
baynezy Avatar answered Sep 21 '22 05:09

baynezy