1

How can I remove /public/ from url and auto force http to https?

my root .htaccess (I created it)

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

public/.htaccess (by default laravel)

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

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

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

But it not works - no auto http to https redirect.

So I neet to combine removing public folder from url and forcing http to https using .htaccess

Teretto
  • 635
  • 2
  • 10
  • 22
  • Possible duplicate of [Laravel 5 – Remove Public from URL](https://stackoverflow.com/questions/28364496/laravel-5-remove-public-from-url) – Script47 Sep 19 '19 at 10:00
  • https://stackoverflow.com/questions/4398951/force-ssl-https-using-htaccess-and-mod-rewrite – Script47 Sep 19 '19 at 10:00
  • Let's try to fix one problem after the other - where do you generate your URLs? Does your VHost configuration point to the root folder of your application, or to your public folder? – Nico Haase Oct 26 '20 at 11:48

4 Answers4

0

I think you need to enable rewrite_url with

$ sudo a2enmod rewrite

and

restart your apache server

$ sudo systemctl restart apache2

Your site-enabled/000-default.conf

 DocumentRoot /var/www/html/yourapp/public

May be you can use

RewriteBase "yourapp/public/"

or refer this answer.

Rename server.php in your Laravel root folder to index.php
Copy the .htaccess file from /public directory to your Laravel root folder.
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0

The problem you're having is that you've set your document root to be the root of the project. In your apache virtual host config you need to set the document root to be the public directory.

For example:

DocumentRoot /var/www/yourproject/public

That way, public doesn't appear in the URL.

If you're using shared hosting you'll want to move the root of your project to above public_html/, delete public_html, then rename your public directory to public_html. You can then go and modify the bootstrap files to use this path. You could also symlink it.

ollieread
  • 6,018
  • 1
  • 20
  • 36
0

Add this two line into your Public Folder htaccess file

 RewriteCond %{HTTPS} !=on
 RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]

Full Code of your public/.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

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

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

    RewriteCond %{HTTPS} !=on
    RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]
</IfModule>
Vaibhavi S.
  • 1,083
  • 7
  • 20
0

Put this code in .htaccess file of root directory of domain for removing 'public :

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

And for redirecting to https from http add following lines at the top in .htaccess file located in public directory of laravel project.

RewriteCond %{HTTP_HOST} your_domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.your_domain.com/$1 [R,L]