I'm redirecting my primary domain to a subfolder and I would like to force my domain to use the HTTPS

With the following code, I would able to redirect my primary domain to a subfolder.

# .htaccess main domain to subfolder redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.

# Do not change this line.
RewriteEngine on

# Change yourdomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$

# Change 'subfolder' to be the folder you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/yourdomain.com/

# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Change 'subfolder' to be the folder you will use for your main domain.
RewriteRule ^(.*)$ /yourdomain.com/$1

# Change yourdomain.com to be your main domain again.
# Change 'subfolder' to be the folder you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.

RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteRule ^(/)?$ yourdomain.com/index.php [L]

But in subfolder yourdomain.com there is one more .htaccess file which has the following code. This code is used to redirect all URI request go through an index.php file (MVC Pattern)

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Now to force the domain to use HTTPS I will write the following code

RewriteEngine on

# PHP -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php70” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php70 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^/?$ "https\:\/\/yourdomain\.com\/" [R=301,L]

I am not sure where should I place the above code to force the domain to use HTTPS. I tried to put it in subfolders .htaccess file but it throws the following error:

The requested URL /yourdomain.com/url=index.php was not found on this server.

I would like to achieve the following things:

  1. Redirect primary domain to a subfolder called yourdomain.com
  2. Redirect all web traffic to an index.php file which would be in the subfolder (MVC Pattern).
  3. Force the domain to use HTTPS instead of HTTP.

Thanks!

0 Answers