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

前端 未结 5 1762
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 05:08

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/

相关标签:
5条回答
  • 2020-12-08 05:38

    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>
      
    0 讨论(0)
  • 2020-12-08 05:41

    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/');
    
    0 讨论(0)
  • 2020-12-08 05:48

    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 讨论(0)
  • 2020-12-08 05:51

    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

    0 讨论(0)
  • 2020-12-08 05:51

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

    0 讨论(0)
提交回复
热议问题