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

Posted May 26, 2018 1k views
Apache PHP Joomla

so far i have this (nothing happens)

RewriteEngine on

RewriteCond %{HTTPHOST} !^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.

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.

2 answers

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>


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 %{SERVERNAME} =www.mydomain.com [OR]
RewriteCond %{SERVER
NAME} =mydomain.com
RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]

i change mydomain.com to www.mydomain.com

RewriteEngine on
RewriteCond %{SERVERNAME} =www.mydomain.com [OR]
RewriteCond %{SERVER
NAME} =www.mydomain.com
RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [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.

by Erika Heidi
This tutorial will show you how to set up a free TLS/SSL certificate from Let’s Encrypt on a Ubuntu 14.04 server running Apache as web server. SSL certificates are used within web servers to encrypt the traffic between server and client, providing extra security for users accessing your application.
Submit an Answer