Secure non-www to secure www redirect

Hey Everyone,

I know this has probably been posted before but i just can’t seem to get it to work.

Here is the code i tried in the HTACCESS file:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^{REQUEST_URI} [R=301,L,NE]

I tried this one as well:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

And this one:

RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

But none of those work. Am i doing something wrong? This is very frustrating. Does it have something to do with MODX?


What is the exact thing you try to do?

Using the .htaccess file Im trying to redirect to using the examples i posted. I’ve done this before but not on MODX and for some reason nothing works. Is there a special way i have to do it in MODX?

Some possibilities:

  1. You don’t have RewriteEngine on at the top of your .htaccess

  2. You’re on an NGINX server (e.g., MODX Cloud) where .htaccess is not used

  3. Your host has disabled .htaccess

  1. So i can confirm that RewriteEngine is on at the top of the .htaccess.

  2. I’m on Apache.

  3. The .htaccess is enabled because i recently tested some 301 redirects and its working.

Here is my .htaccess file

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

# rewrite rule for server-status to prevent 404 error
# RewriteCond %{REQUEST_URI} !=/server-status

# Force https
#RewriteCond %{HTTP:clientproto} !https
#RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# Rewrite -> -- used with SEO Strict URLs plugin
#RewriteCond %{HTTP_HOST} .
#RewriteCond %{HTTP_HOST} !^example-domain-please-change\.com [NC]
#RewriteRule (.*)$1 [R=301,L]
# or for the opposite -> use the following
#RewriteCond %{HTTP_HOST} .
#RewriteCond %{HTTP_HOST} !^www\.example-domain-please-change\.com [NC]
#RewriteRule (.*)$1 [R=301,L]

# Rewrite secure requests properly to prevent SSL cert warnings, e.g. prevent 
# when your cert only allows
#RewriteCond %{SERVER_PORT} !^443
#RewriteRule (.*)$1 [R=301,L]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

You may have tried this, but this is what I’d prefer. My new guess is that you’re not escaping the dot(s) in the RewriteCond line with a backslash:

RewriteCond %{HTTP_HOST} ^www\.example\.com$

Ah, may be the dots like Bob said.