Redirect non-www to www through secure connection (https/ssl)


I want to redirect to

I succeeded in redirecting http to https like this (in default-ssl.conf file):

<pre> <VirtualHost> RewriteEngine on ReWriteCond %{SERVER_PORT} !^443$ RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [NC,R,L]


<VirtualHost default:443> RewriteEngine on ServerAdmin webmaster@localhos ServerName ServerAlias DocumentRoot /var/www/html </pre>

this works fine until I type my domain without www, then I get the error message “this site is untrusted, the certificate is only valid to

My environment:

will appreciate you help thanks,

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Why not simply:

NameVirtualHost *:80
<VirtualHost *:80>
   Redirect permanent /

<VirtualHost _default_:443>
   DocumentRoot /var/www/html
   SSLEngine On
# etc...

I found the solution, for the people who suffered as me in finding a solution… here is the answer: <br> <br>VirtualHost <br> <br> RewriteEngine on <br> ReWriteCond %{SERVER_PORT} !^443$ <br> RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [NC,R,L] <br> <br>VirtualHost default:443 <br> <br> ServerAdmin webmaster@localhost <br> ServerName <br> ServerAlias <br> DocumentRoot /var/www/html <br> RewriteEngine on <br> RewriteCond %{HTTP_HOST} !^(www.)?(.+) <br> RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L] <br> <br>Enjoy! <br>

sorry the post does not accept programatic brackets. so first block of code is for <br>VirtualHost <br> <br>the second block is for <br>VirtualHost default:443