Search code examples
apache.htaccesszend-framework2centoshttpd.conf

Apache - how to make http requests into https only?


How to tell if http://ttt.com or http://www.ttt.com is used by the user, redirect it to https://www.ttt.com ?

httpd.conf:

<VirtualHost *:80>
 ServerName www.ttt.com
 ServerAlias ttt.com
 DocumentRoot /home/www/html/ttt/public
 <Directory /home/www/html/ttt/public>
    #Options ExecCGI
    #AddDefaultCharset utf-8
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
 </Directory>
</VirtualHost>

.htaccess :

RewriteEngine On
############################################
## always send 404 on missing files in these folders
#RewriteCond %{REQUEST_URI} !^/(files)/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Solution

  • Try the following Code at the main directory .htaccess file :

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]