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

标签 .htaccess mod-rewrite laravel laravel-routing

设置

我正在尝试在我的服务器上的此目录中安装 laravel 5 应用程序:

public_html/myapp/

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

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

我创建了此 .htaccess 规则并将其放置在 myapp 文件夹中以便重定向请求:

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

问题:

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

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

而不是漂亮的 URL,例如:

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

我需要在 .htaccess 文件中更改哪些内容才能使 URL 正常工作?

<小时/>

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

更新:当我尝试访问 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))

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

<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>

最佳答案

您可以使用符号链接(symbolic link)

  1. 将您的 Laravel 应用放在其他位置(public_html 文件夹之外)
  2. 在 public_html 文件夹中创建应用公共(public)文件夹的符号链接(symbolic link)

    cd/path/to/your/public_html

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

You can read more about symlinks here.

关于.htaccess - 使用 htaccess 在子目录中安装 Laravel 5 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497575/

相关文章:

apache - 此网页有重定向循环

.htaccess - 客户端站点的 htaccess 被黑 - 不完全确定这会做什么或从这里做什么

php - 使用 htaccess 文件的多种语言的友好 php URL(从 URL 检测语言)

laravel - 社会名流 Google redirect_uri_mismatch

php - 将结果分为 5 列

apache - 让 FilesMatch 和 Satisfy Any 能够处理子目录

.htaccess - 在 htaccess 中重写 URL 时添加 .html?

apache - 使用 mod_rewrite 匹配奇数或偶数

apache - mod_rewrite : allow redirect but prevent direct access

php - 将 Excel 导入 mySQL 花费的时间太长