45

I have a classic Larevel 5 project structure and I need to redirect all requests to public/.

I am on a classic hosting environment so public/ is a subfolder of my document root.

I shall imagine it can be done via .htaccess but I still need to figure out how. Anyone can help?

Thanks

1

6 Answers 6

80

There are two solutions:

1. Using .htaccess with mod_rewrite

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

2. You can add a index.php file containing the following code and put it under your root Laravel folder (public_html folder).

<?php
header('Location: public/');
5
  • This doesn't seem to work 100% like it should, if I go to localhost it says not found whilst when I go to localhost/index.php it gives the laravel homepage. It seems like you specifically have to type the full filename then. Any solutions for this behavior?
    – JohannesB
    Mar 30, 2018 at 8:14
  • @JohannesB what's your development environment? Mar 30, 2018 at 16:27
  • LAMP stack, x86_64 Linux 4.15.12-1-ARCH, Apache/2.4.29, mysql Ver 15.1 Distrib 10.1.31-MariaDB , PHP 7.2.3
    – JohannesB
    Mar 30, 2018 at 16:44
  • 1
    This is what I am using now, it redirects to the index.php page to solve this issue, but it still seems weird and not the correct way to go
    – JohannesB
    Mar 30, 2018 at 16:45
  • 3
    Following my preferred solution (your option 1)@MuhammadSumonMollaSelim I find that if I point to paths on my destination server that end with a back slash I get /public/ in the url. ie a request to www.test.com/page/ becomes www.test.com/public/page
    – AdamJones
    Sep 16, 2019 at 1:15
25

You don't need to change anything in Laravel's default public/.htaccess file.

Just create a new .htaccess in the same level your public folder and add the following content to it:

DirectoryIndex index.php

RewriteEngine On 
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

That simple!

0
11

This is an extract from another answer which may also help you.

--

  • Modify your public_html/.htaccess to redirect all requests to the public subfolder.

    # public_html/.htaccess
    
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect All Requests To The Subfolder
        RewriteRule ^ /public
    
    </IfModule>
    
  • Make sure you have the proper public_html/public/.htaccess (GitHub).

    # public_html/public/.htaccess
    
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization}
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    </IfModule>
    
2
  • its also working with CodeIgniter 4. Thanks for help.
    – PHP Ninja
    Dec 7, 2018 at 5:59
  • This laravel crap changes with every release. I had this in a Laravel 5 installation. Same thing doesn't work at all in Laravel 7 installation. And yeah, the XAMPP stuff is all defined exactly the same. But now, Yii and Slim, they will work right out of the box. Laravel always gives me problems.
    – McAuley
    May 15, 2021 at 3:25
4

If you use cPanel, then:

1.Go to folder: /var/cpanel/userdata/my_domain

2.Edit the both domains: my.domain and my.domain_SSL

Add to the documentroot section /public:

documentroot: /home/user/public_html/public

3.Rebuild Apache config: /scripts/rebuildhttpdconf && service httpd restart

1
  • Not what op asked for. May 11, 2021 at 8:46
4

if you using apache , add .htaccess file to your root directory with this lines :

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

and make sure your apache redirect and rewrite modules is working fine .
you can use vhost to redirect the all request to your public directory with set public as root of your project .

0

The ideal scenario is to have /home/user/public as a symlink from /home/user/laravel/public.

Not the answer you're looking for? Browse other questions tagged or ask your own question.