D
D
Dmitry2020-02-03 09:45:30
htaccess
Dmitry, 2020-02-03 09:45:30

How to redirect from HTTP to HTTPS in Laravel?

I'm trying to set up a redirect on a laravel site from http to https
in the root of the site .htaccess content:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


And here is the content of .htaccess in the public folder:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On 
    
    RewriteCond %{HTTPS} off
    
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # 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} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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


The redirection works, but there was a problem that the word "public" constantly hangs in the link, that is:
https://my-site.ru/ public /login

How to get rid of this word in the address?

- I can't redirect the site directly to public - hosting does not have such an opportunity.
- to create a symlink is also not possible for the hoster.
I understand, the hoster is in the furnace), but still, is there a solution, how to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zorca, 2020-02-03
@zorca

In this case, we can only advise you to move the public content to the root of the site, since this folder loses its security purpose. Do not forget to close all configs and .env.

# Block access to hidden files (except the /.well-known/ dir)
location ~ /(?!.well-known)(\.)\w+ {
  deny all;
}

Redirecting to https is ideally done on Cloudflare so that these requests do not even reach the site.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question