https redirection not working

#1
I have some issues with http redirecting to https.
I have .htaccess file with the following:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/%{REQUEST_URI} [R,L]

When I insert in the address bar https:// example.com it works just fine. However, if I change it to http:// example.com it does not redirect to https, but shows the server default page.
Any suggestions?

default.png
 

serpent_driver

Well-Known Member
#2
Code:
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
 
  RewriteCond %{HTTP_HOST} ^www\.
  RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
 
#3
Changed the content of .htaccess with this suggestion, but the result is the same. Typing http:// example.com into the address bar still does not redirect to https and shows the server default page.
 
#5
In the Rewrite tab for the vhost I have Enable Rewrite = Yes and Auto Load from .htaccess = Yes. The other sections are blocked by Cpanel (Auto Load from .htaccess and Rewrite Rules). Should I insert
<directory>
AllowOverride all
</directory>
into the vhost conf from Cpanel?
 
#7
Still not working.
This is what Cpanel shows me when I want to see the vhost conf

Code:
docRoot                   $VH_ROOT/public_html
vhDomain                  $VH_NAME
vhAliases                 www.$VH_NAME
adminEmails               admin@example.com
enableGzip                1
enableIpGeo               1

errorlog $VH_ROOT/logs/$VH_NAME.error_log {
  useServer               0
  logLevel                WARN
  rollingSize             10M
}

accesslog $VH_ROOT/logs/$VH_NAME.access_log {
  useServer               0
  logFormat               "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""
  logHeaders              5
  rollingSize             10M
  keepDays                10
  compressArchive         1
}

index  {
  useServer               0
  indexFiles              index.php, index.html
}

scripthandler  {
  add                     lsapi:example2503 php
}

extprocessor example2503 {
  type                    lsapi
  address                 UDS://tmp/lshttpd/example2503.sock
  maxConns                10
  env                     LSAPI_CHILDREN=10
  initTimeout             600
  retryTimeout            0
  persistConn             1
  pcKeepAliveTimeout      1
  respBuffer              0
  autoStart               1
  path                    /usr/local/lsws/lsphp82/bin/lsphp
  extUser                 example2503
  extGroup                example2503
  memSoftLimit            2047M
  memHardLimit            2047M
  procSoftLimit           400
  procHardLimit           500
}

context /.well-known/acme-challenge {
  location                /usr/local/lsws/Example/html/.well-known/acme-challenge
  allowBrowse             1

  rewrite  {
    enable                1
  }
  addDefaultCharset       off
}

rewrite  {
  enable                  1
  autoLoadHtaccess        1
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
 
  RewriteCond %{HTTP_HOST} ^www\.
  RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
}

vhssl  {
  keyFile                 /etc/letsencrypt/live/example.com/privkey.pem
  certFile                /etc/letsencrypt/live/example.com/fullchain.pem
  certChain               1
  enableECDHE             1
  renegProtection         1
  sslSessionCache         1
  enableSpdy              15
  enableStapling          1
  ocspRespMaxAge          86400
}

module cache {
storagePath /usr/local/lsws/cachedata/$VH_NAME
}
<directory>
AllowOverride all
</directory>
 

serpent_driver

Well-Known Member
#8
You must add this code to .htaccess and not to the conf file and replace example.com with the domain name used.

Code:
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
 
  RewriteCond %{HTTP_HOST} ^www\.
  RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
 
#9
I don't know how that code got into vhost conf. I deleted it. .htaccess file contains
Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I obviously changed example com to my actual address.
But, still no luck. It gets frustrating that such an apparently simple task is not working for me.
I forgot to mention that every time I changed the .htaccess file or the vhost conf I restarted the server.
 
Top