0

I am trying to accomplish the following... if a user comes to the site via 123abc.com (with or without www), and 123abc.com/hello (with or without www) they should automatically be taken to https://www.123abc.com/URI_IF_NEED ...

this is what I have so far. the base domain redirects correctly but any URL structures with pages/dir following it does not.

trying to achieve https://www.123abc.com and/or https://www.123abc.com/hello

ServerName www.123abc.com
ServerAlias 123abc.com
DocumentRoot "/mnt/var/www/html/"

RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
acctman
  • 4,229
  • 30
  • 98
  • 142

1 Answers1

1

You don't need a complex rewrite for this. Just create different virtual servers and let non-www redirect on www(443/https) and a single one for (80/http)

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

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

<VirtualHost *:443>
    ServerName www.example.com
    # real server configuration
</VirtualHost>

PS: Credits apache redirect from non www to www

If you still want to write the way you were, then you can write it like below

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI}   [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}   [R=301,L]

Tested on online tester

https://htaccess.madewithlove.be/

Different test results

Test 1

Test 2

Test 3

Test 4

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • I think the "HTTP:X-Forwarded-Proto" line is redundant. – Jannes Botis Apr 26 '18 at 11:19
  • I only kept it because of the OP's config, it may be the request is proxied to apache from another webserver. So not sure if it could required or not as it may depend on the environment setup. But yes I would not use it until unless it is really needed – Tarun Lalwani Apr 26 '18 at 11:24
  • You are correct. "X-Forwarded-Proto" is a "de facto standard for identifying the originating protocol of an HTTP request, since a reverse proxy (or a load balancer) may communicate with a web server using HTTP even if the request to the reverse proxy is HTTPS". But it appears to me that the 1st "RewriteCond" should be moved to the 2nd rule. – Jannes Botis Apr 26 '18 at 13:05
  • @JannesBotis, would to understand the reasoning behind the same? – Tarun Lalwani Apr 26 '18 at 13:16
  • What if the header is missing or is http and domain is eg www.abc.com, would that redirect to https://www.www.abc.com? – Jannes Botis Apr 26 '18 at 15:32
  • @JannesBotis, see the updated answer, let me know if there is a scenario that this missed? – Tarun Lalwani Apr 26 '18 at 15:38