Report this

What is the reason for this report?

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 on May 26, 2018

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.



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.

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 %{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.

To redirect non-www to www for your domain using Apache, you need to correctly configure either the .htaccess file or the Apache virtual host configuration. Here’s a step-by-step guide for both approaches.

Option 1: Using .htaccess for Redirect

If you’re using .htaccess, try the following configuration:

  1. Edit the .htaccess File in your document root (/var/www/mydomain.com):
sudo nano /var/www/mydomain.com/.htaccess
  1. Add the Following Rewrite Rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This will redirect any non-www requests to the www version, regardless of whether the request is over HTTP or HTTPS.

  1. Save and Exit, then reload Apache.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.