2

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!

namelivia
  • 2,657
  • 1
  • 21
  • 24
Dhaval Naphade
  • 555
  • 2
  • 21

4 Answers4

4

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>
Dhaval Naphade
  • 555
  • 2
  • 21
0

Try this code:

RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R]
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
kalehmann
  • 4,821
  • 6
  • 26
  • 36
Gaurav Gupta
  • 1,588
  • 2
  • 14
  • 21
0
<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.

Learner
  • 723
  • 3
  • 13
  • 36
0

Tried every solution but this worked for me on Laravel 6.0, use this by creating .htaccess file in root folder rather than /public/.htaccess file.

Options +SymLinksIfOwnerMatch
RewriteEngine On

# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
RewriteRule ^ index.php [L]
macgadger
  • 11
  • 3