skip to Main Content

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!

4

Answers


  1. Chosen as BEST 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>
    

  2. Try this code:

    RewriteCond %{SERVER_PORT} ^80$
    RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
    RewriteRule ^/(.*)$ http://%1/$1 [R]
    RewriteRule ^/?$ "https://example.com/" [R=301,L]
    
    Login or Signup to reply.
  3. <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.

    Login or Signup to reply.
  4. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search