php – Redirect website http to https in laravel in shared hosting using htaccess-ThrowExceptions

Exception or error:

I have a Laravel project. and I used Godaddy. Recently, I installed an SSL certificate on my website so when I type https://example.com in the browser it works but when I write example.com, it connects by HTTP.

To fix that, I added these lines to my root folder’s .htaccess file:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

But this redirects to me https://example.com/public.

I added two htaccess one is in the root folder and the second one is in public folder.

first htaccess code :

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L,NC]

Second htaccess code is :

Options -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^ %1 [L,R=301]


# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

How can I achieve this https://example.com.

Thanks in advance!

How to solve:
<IfModule mod_rewrite.c>
    RewriteEngine on
    Options +FollowSymlinks
    #redirect http non-www to https://www
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?your-domain\.com$
    RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
    #redirect https non-www to www
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^your-domain\.com$
    RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
</IfModule>

I am using this to redirect.

Answer:

Try this code:

RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R]
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]

Answer:

Here is the Solution,

<IfModule mod_rewrite.c>
   RewriteEngine On
   # Force SSL
   RewriteCond %{HTTPS} !=on
   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
   # Remove public folder form URL
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Leave a Reply

Your email address will not be published. Required fields are marked *