I want to redirect my naked domain to https with www like => https://www.example.com
I am using Ubuntu 16.04, apache, Let’s Encrypt.
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!
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.
Rewrite rules will help you with this. It’s very powerful utility, you do almost all URL manipulations with it.
We will added needed rules to Apache config. If you use default configuration, it’s stored in file called
000-default-le-ssl.conf
. Open it with text editor:By default, this is content of file:
You need to add following rules:
First rule -
RewriteEngine On
, enables rewrite engine for you. Second one is condition, if it’s you are not accessing viawww.example.com
,RewriteRule
will execute. Third one is rule, it rewrites your URL to correct one -www.example.com
When you add it, your file will look like:
It requires you to have
rewrite
module enabled. To make sure, execute this:To make this changes in effect - you’ll have to restart Apache:
This can be done via
.htaccess
to, but as you have access to config files, you can use config files. :)This is done for @ayan, now let’s go on @newbie part. =) This depends on your setup. If you followed How To Secure Nginx with Let’s Encrypt on Ubuntu 16.04, you have 2 server blocks. One to redirect, and one for HTTPS. In that case, you just need to add
www
toreturn
. Let’s assume you have redirect block like this one:If you change
https://$server_name$request_uri;
tohttps://www.$server_name$request_uri;
, you’ll get desired result. And block will look as this:Hi @xMudrii @ayan, I too want to do this, but somehow, I can’t figure out the default 301 redirection to example.com.
So, I have setup the LetsEncrypt SSL and it’s working fine, but instead of redirecting to www.example.com, all the redirection is pointing on example.com. and I am unable to figure it out.
I added your code, but that creates the redirection loop.
Edit (Answer) : I did fixed it from Wordpress backend, but updating the www in site url and wordpress url.
Thanks… It’s works but when url www.example.com it not redirect “https://www.example.com” please tell me how i will do it.