你如何将所有请求重定向到 Laravel 5 中的 public/ 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38040502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:26:10  来源:igfitidea点击:

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

.htaccesslaravel

提问by nourdine

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

我有一个经典的 Larevel 5 项目结构,我需要将所有请求重定向到public/.

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

我在一个经典的托管环境中,所以public/是我的文档根目录的子文件夹。

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

我可以想象它可以通过 .htaccess 完成,但我仍然需要弄清楚如何。任何人都可以帮忙吗?

Thanks

谢谢

回答by Muhammad Sumon Molla Selim

There are two solutions:

有两种解决方案:

1. Using .htaccess with mod_rewrite

1. 使用 .htaccess 和 mod_rewrite

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

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

2. 您可以添加一个包含以下代码的 index.php 文件,并将其放在您的 Laravel 根文件夹(public_html 文件夹)下。

<?php
header('Location: public/');

回答by Qevo

This is an extract from another answerwhich may also help you.

这是另一个答案的摘录,也可能对您有所帮助。

--

——

  • Modify your public_html/.htaccessto redirect all requests to the publicsubfolder.

    # 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 ^(.*)/$ / [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>
    
  • 修改您public_html/.htaccess以将所有请求重定向到public子文件夹。

    # 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>
    
  • 确保您拥有正确的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 ^(.*)/$ / [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>
    

回答by Gilberto Albino

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

您不需要更改 Laravel 默认的public/.htaccess文件中的任何内容。

Just create a new .htaccessin the same level your publicfolder and add the following content to it:

只要创建一个新的.htaccess在同级别的公共文件夹和添加以下内容吧:

DirectoryIndex index.php

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

That simple!

就这么简单!

回答by amstel

If you use cPanel, then:

如果您使用 cPanel,则:

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

1.转到文件夹: /var/cpanel/userdata/my_domain

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

2.编辑两个域: my.domain 和 my.domain_SSL

Add to the documentroot section /public:

添加到 documentroot 部分/public

documentroot: /home/user/public_html/public

文档根目录:/home/用户/public_html/public

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

3.重建Apache配置: /scripts/rebuildhttpdconf && service httpd restart

回答by user2094178

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

理想的情况是有/home/user/public从一个符号链接/home/user/laravel/public