1

I can't seem to get myredirects to work as desired. My objectives are:

  1. Redirect http://example.com to https://www.example.com
  2. Redirect http://www.example.com to https://www.example.com

Objective 1 has aleady been achieved, but the problem is redirection http://www.example.com to https://www.example.com, and its actually resulting to a 404 error. I used Ryte to identify the issue which is as below:

https://enmowe.com/ (403) is not reachable
http://enmowe.com/ (301) is forwarding to https://www.enmowe.com/ (200)
https://www.enmowe.com/ (200) is available
http://www.enmowe.com/ (404) is not reachable

I have tried the following in my Apache VirtualHost:

Method 1.

    <If "%{HTTP_HOST} != 'www.enmowe.com'">
        Redirect "/" "https://www.%{HTTP_HOST}/$1"
    </If>

    <If "%{SERVER_PROTOCOL} != 'HTTPS'">
        Redirect "/" "https://www.%{HTTP_HOST}/$1"
    </If>

Method 2.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.\.enmowe\.com)?$
RewriteRule (.*) https://www.enmowe.com/$1 [R=301,L,NC]

Method 3.

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

Kinda feel lost right now

Chris Mwai
  • 91
  • 1
  • 14

1 Answers1

2

You want to redirect if the protocol isn't right or the hostname. So you should formulate these two conditions:

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://www.enmowe.com/$1 [R=301,L,NC]

RewriteCond %{HTTP_HOST} !^(www.\.enmowe\.com)?$
RewriteRule (.*) https://www.enmowe.com/$1 [R=301,L,NC]

The fact that https://enmowe.com isn't reachable at all is not tied to this. If you want it to be reachable and you have the right certificate add the hostname as an ServerAlias to the definition of the https server.

BTW: Please remove virtual-hosts tag as it's not related to your question.

blafasel
  • 1,091
  • 14
  • 25
  • Thanks, this only works partially to. It sets the url to https://www when its http://enmowe... or http://www.enmowe... However, https://enmowe... still doesn't redirect to https://www.enmowe... – Chris Mwai Oct 18 '17 at 19:09
  • This may be due to the regex i copied from your question. I didn't test. Enable [rewrite debugging](https://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite) and have a look whether the second condition matches. – blafasel Oct 18 '17 at 19:37
  • This is what I have in the enmowe.com.com vhost file: `RewriteEngine on RewriteCond %{HTTPS} !=on [OR] RewriteCond %{HTTP_HOST} !^(www.\.enmowe\.com)?$ RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]` This is based on your answer. However, still `https://enmowe.com` doesn't seem to get redirected. I also added: `RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} !^(www.\.enmowe\.com)?$ RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]` Really strange. I created a vhost for `www.enmowe.com`. – Chris Mwai Oct 19 '17 at 06:14
  • However, as I stated before `http://enmowe.com` and `http://www.enmowe.com` are being redirected to `https://www.enmowe.com` as desired. – Chris Mwai Oct 19 '17 at 06:19