使用 htaccess 在子目录中安装 Laravel 5 应用程序 [英] Install Laravel 5 app in subdirectory with htaccess

查看:14
本文介绍了使用 htaccess 在子目录中安装 Laravel 5 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置

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

public_html/myapp/

而且我希望通过这个 URL 访问它:

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

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

RewriteEngine on重写规则 ^(.*)$ public/$1 [L]

问题:

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

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

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

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

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

<小时>

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

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

incompiled.php 行 7793在 RouteCollection->match(object(Request)) incompiled.php 行 7066在 Router->findRoute(object(Request)) incompiled.php 行 7038在 Router->dispatchToRoute(object(Request)) incompiled.php 行 7030在 Router->dispatch(object(Request)) incompiled.php line 1989在内核->IlluminateFoundationHttp{closure}(object(Request))

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

<IfModule mod_negotiation.c>选项 - 多视图</IfModule>重写引擎开启# 重定向尾部斜杠...重写规则 ^(.*)/$/$1 [L,R=301]# 处理前端控制器...RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^ index.php [L]</IfModule>

解决方案

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

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

    cd/path/to/your/public_html

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

<块引用>

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

The Setup

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

public_html/myapp/

And I want it to be accessed at this URL:

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

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

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

Problem:

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

  • 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

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


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.

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

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->IlluminateFoundationHttp{closure}(object(Request))

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

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

    RewriteEngine On

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

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

</IfModule>

解决方案

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

You can read more about symlinks here.

这篇关于使用 htaccess 在子目录中安装 Laravel 5 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆