使用 htaccess 在子目录中安装 Laravel 5 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30497575/
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:02:29  来源:igfitidea点击:

Install Laravel 5 app in subdirectory with htaccess

.htaccessmod-rewritelaravellaravel-routing

提问by Ben Cole

The Setup

设置

I am trying to install a laravel 5 app in this directory on my server:

我正在尝试在我的服务器上的这个目录中安装一个 Laravel 5 应用程序:

public_html/myapp/

public_html/myapp/

And I want it to be accessed at this URL:

我希望通过以下 URL 访问它:

http://www.example.com/myapp/

http://www.example.com/myapp/

I have created this .htaccess rule and placed it in the myappfolder in order to redirect requests :

我创建了这个 .htaccess 规则并将它放在myapp文件夹中以重定向请求:

RewriteEngine on
RewriteRule ^(.*)$ public/ [L]

Problem:

问题:

In my browser when I try to access the app I get redirected URLs like:

在我的浏览器中,当我尝试访问该应用程序时,我会收到如下重定向的 URL:

  • http://www.example.com/myapp/public/index.php
  • http://www.example.com/myapp/public/index.php/dashboard
  • http://www.example.com/myapp/public/index.php
  • http://www.example.com/myapp/public/index.php/dashboard

instead of nice pretty URLs like:

而不是漂亮漂亮的网址,如:

  • http://www.example.com/myapp/
  • http://www.example.com/myapp/dashboard
  • http://www.example.com/myapp/
  • http://www.example.com/myapp/dashboard

What do I need to change in my .htaccess file to get proper URLs working?

我需要在我的 .htaccess 文件中更改什么才能使正确的 URL 工作?



Update: The laravel app can be moved elsewhere if needed, as long as the desired URL structure above (with the app accessible at example.com/myapp/) can be achieved.

更新:如果需要,laravel 应用程序可以移动到其他地方,只要可以实现上面所需的 URL 结构(应用程序可从 example.com/myapp/ 访问)。

Update: When I try to access example.com/myapp/dashboardI receive the following error message in the browser: NotFoundHttpException in compiled.php line 7793:with a stack trace that starts with:

更新:当我尝试访问时,example.com/myapp/dashboard我在浏览器中收到以下错误消息:NotFoundHttpException in compiled.php line 7793:堆栈跟踪以以下内容开头:

in compiled.php line 7793
at RouteCollection->match(object(Request)) in compiled.php line 7066
at Router->findRoute(object(Request)) in compiled.php line 7038
at Router->dispatchToRoute(object(Request)) in compiled.php line 7030
at Router->dispatch(object(Request)) in compiled.php line 1989
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))

Update: The following .htaccess file is located in the public folder:

更新:以下 .htaccess 文件位于公共文件夹中:

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ / [L,R=301]

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

</IfModule>

回答by atefth

What you can do is use a symlink.

您可以做的是使用符号链接

  1. Place your laravel app somewhere else (outside the public_html folder)
  2. Create a symlink of your app's public folder inside the public_html folder

    cd /path/to/your/public_html

    sudo ln -s /path/to/your/laravel_app/public myapp

  1. 将您的 Laravel 应用程序放在其他地方(在 public_html 文件夹之外)
  2. 在 public_html 文件夹中创建应用程序公共文件夹的符号链接

    cd /path/to/your/public_html

    须藤 ln -s /path/to/your/laravel_app/public myapp

You can read more about symlinks here.

您可以在此处阅读有关符号链接的更多信息。

回答by Sh1d0w

You can easily achieve this by adding additional .htaccessfile in the myappfolder (this will redirect all of your requests in myappfolder to the publicas it should):

您可以通过.htaccessmyapp文件夹中添加其他文件来轻松实现这一点(这会将文件夹中的所有请求重定向myapppublic它应该的位置):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/ [L]
</IfModule>

You should also modify the .htaccessin your myapp/publicdirectory, like this:

你也应该修改.htaccess你的myapp/public目录,如下所示:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On    
    RewriteBase /myapp/

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

The change here is that you rewrite the base path RewriteBase /myapp/

这里的变化是你重写了基本路径 RewriteBase /myapp/

回答by jose miguel rivera rodríguez

I was having the same issue, but with the RewriteBaseparameter it was possible to fix it.

我遇到了同样的问题,但是使用RewriteBase参数可以修复它。

This is my .htaccess:

这是我的.htaccess:

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

    RewriteEngine On
    RewriteBase /my_app/

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ / [L,R=301]

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

回答by gradosevic

This example should work for following cases:

这个例子应该适用于以下情况:

Follow these steps:

按着这些次序:

  1. Move everything from myapp/publicfolder to myapp/folder
  2. Open myapp/index.phpand update bootstrap paths to require __DIR__.'/bootstrap/
  3. Open .env and update to your base url like: APP_URL=http://example.com/myapp
  4. Update paths in all views and layout (also in the code if you have any) to use base url, like this: src="{{URL::to('/js/app.js')}}"
  1. 将所有内容从myapp/public文件夹移动到myapp/文件夹
  2. 打开myapp/index.php并更新引导程序路径以要求 __DIR__.'/bootstrap/
  3. 打开 .env 并更新到您的基本网址,例如:APP_URL= http://example.com/myapp
  4. 更新所有视图和布局中的路径(如果有的话,也在代码中)以使用基本 url,如下所示: src="{{URL::to('/js/app.js')}}"

It's always recommended to separate public folder from the rest of your app. You can add some protection to Laravel folders by adding .htaccess files inside them that will contain "Deny from all".

始终建议将公共文件夹与应用程序的其余部分分开。您可以通过在 Laravel 文件夹中添加包含“拒绝所有人”的.htaccess 文件来为 Laravel 文件夹添加一些保护。