Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you redirect all request to public/ folder in laravel 5

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

like image 755
nourdine Avatar asked Jun 26 '16 16:06

nourdine


People also ask

Where does .htaccess file go Laravel?

htaccess file for redirecting to Laravel's public folder. In Laravel the path for serving your web page is in the /public folder. By default after installing Laravel and navigating in a browser to the URL you will see a directory listing of all the Laravel files.

What is htaccess in Laravel?

htaccess file that is used to allow URLs without index. php . If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.

What is public folder in Laravel?

The files and folders in laravels public folder are meant to be web accessible. For security, all other files and folders in the laravel framework should not be web accessible. Moving the index. php to laravels root will break the framework and defy best practices.

How do I change directory in Laravel?

Add a line to change the default public path ( mix. config. publicPath ), and change the public folder to your desired public folder name in the Laravel mix configuration.


3 Answers

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/'); 
like image 119
Muhammad Sumon Molla Selim Avatar answered Sep 29 '22 16:09

Muhammad Sumon Molla Selim


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!

like image 33
Gilberto Albino Avatar answered Sep 29 '22 17:09

Gilberto Albino


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>
    
like image 40
Qevo Avatar answered Sep 29 '22 17:09

Qevo