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

May 26, 2018 421 views
PHP Apache 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.

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.
  • opps [erase/edit] it's the lower part:

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

    • edit this add www to RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]

    and the vhost setting is:

    <VirtualHost *:80>

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

    </VirtualHost>

    *lol it's a mess i can't edit my past reply.

Have another answer? Share your knowledge.