0

The redirect with .htaccess of the main domain without "www" like "mydomain.com" doesn't work, leaving the address in the ordinary http://, so what's wrong with this code? and how to force the redirect of all addresses to https://?

ErrorDocument 404 /404.php

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.(.*) [NC] 
RewriteRule ^(.*) https://%1/$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
Lucas
  • 523
  • 2
  • 10
  • 20
Kleber Germano
  • 702
  • 3
  • 10
  • 25
  • Where's the code for that? You are only redirecting requests starting with `www` – Nico Haase Aug 20 '19 at 08:52
  • I've a index.php that handle with the requests, my only issue is that only the www was redirecting to https as you pointed, and as my knowledge in htaccess is really poorly I stucked with this problem, to redirect the http, something like this %{HTTP_HOST} ^http.(.*) [NC] exist ? – Kleber Germano Aug 20 '19 at 09:18
  • 1
    You should check the vocabulary ;) `HTTP_HOST` contains the host. That might start with `http`, but that is not common. Usually it contains `www.domain.com` – Nico Haase Aug 20 '19 at 09:46
  • Possible duplicate of [Redirect non-www to www in .htaccess](https://stackoverflow.com/questions/12050590/redirect-non-www-to-www-in-htaccess) – Nico Haase Aug 20 '19 at 09:46
  • Possible duplicate of [.htaccess redirect www to non-www with SSL/HTTPS](https://stackoverflow.com/questions/1478173/htaccess-redirect-www-to-non-www-with-ssl-https) – Lucas Aug 20 '19 at 17:55

1 Answers1

1

Check these modified rules forcing without www and ssl

RewriteEngine On

# force to non-www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC] 
RewriteRule ^(.*) https://%1/$1 [R=301,L]

# force to ssl
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

ErrorDocument 404 /404.php
JarekBaran
  • 1,311
  • 2
  • 7
  • 12