Question

Help, can anyone show me a very concrete way to redirect a non-www to www..some say change .htacess & mydomain.com.conf alias

so far i have this (nothing happens)

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www. RewriteCond %{HTTPS}s on(s)|offs() RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

ServerAdmin admin@mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/mydomain.com

i read and tried everything i saw in the net but of no avail.


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.

thanks rushpro…im going to try this…xx . . . error…when i restart the apache2 i got failed & when i checked the error log (fatal error and some sort of error things i can’t relate) . . . anyway i got it worked…it is so simple…it goes like this:

you make vhost settings:

<VirtualHost *:80>

ServerName example.com
Redirect permanent / https://example.com/
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

</VirtualHost>

and then, you go Let’s Encrypt https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-14-04

after that, you recheck you’re mydomain.com.conf (where you put you’re vhost settings) in the lower part you will get settings like this before the end of </VirtualHost>:

RewriteEngine on RewriteCond %{SERVER_NAME} =www.mydomain.com [OR] RewriteCond %{SERVER_NAME} =mydomain.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

i change mydomain.com to www.mydomain.com

RewriteEngine on RewriteCond %{SERVER_NAME} =www.mydomain.com [OR] RewriteCond %{SERVER_NAME} =www.mydomain.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

i checked chrome and firefox so far it works for me…anyway thanks for your comment it gives me an idea.

It’s easier this way…

<VirtualHost *:80>
	    
    ServerName example.com
    Redirect permanent / https://example.com/
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>