【问题标题】:How to use Laravel app in subfolder of Apache User Directory如何在 Apache 用户目录的子文件夹中使用 Laravel 应用程序
【发布时间】:2015-09-12 09:22:02
【问题描述】:

我正在尝试为我的一位用户在 Web 根目录的子文件夹中安装 Laravel 4 应用程序。我知道通过网络访问整个框架存在重大安全问题。我计划在了解如何使其发挥作用后单独解决这些问题。

我已经尝试了所有我能想到的 Apache 别名和重写规则的组合,但似乎没有任何效果。这是我的目录结构:

/home/username/public_html
    .htaccess
    my_laravel_app/
        app/
        bootstrap/
        public/
            .htaccess
            index.php
            robots.txt
            ...etc (standard Laravel 4 public folder contents)
        vendor/
        ...etc (standard Laravel 4 files and folders)

第一个.htaccess 文件,直接列在用户public_html 目录下的文件,包含以下内容:

AuthType Basic
AuthName Private
Authuserfile /path/to/.htpasswd
Require valid-user

第二个 .htaccess 是标准的 Laravel 4 .htaccess 文件。它包含以下内容:

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

我想基本上将这个my_laravel_app/public 目录视为一个 webroot,以便 Laravel 应用程序的运行方式与直接在域上正常安装 Laravel 的方式相同。

我相信我只需要编辑第一个 .htaccess 文件以包含将任何请求发送到 /my_laravel_app 到 my_laravel_app/public 的重写规则,但我似乎无法让它工作。有人可以帮忙吗?

【问题讨论】:

标签: php apache .htaccess mod-rewrite laravel-4


【解决方案1】:

这可能因您的设置而异,但基本配置是:

RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/my_laravel_app/public/%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/my_laravel_app/public/%{REQUEST_URI} -f
RewriteRule . public/%{REQUEST_URI} [L]

RewriteRule . public/index.php [L]

并将 my_laravel_app 作为 .htaccess 放入其中以限制条件的复杂性。希望您能理解。

【讨论】:

  • 谢谢。您能否澄清一下“并将 my_laravel_app 作为 .htaccess 放入以限制条件的复杂性”的意思。您是说您显示的代码应该放在my_laravel_app 目录下的.htaccess 文件中?
  • 在 my_laravel_app 中是理想的。没有必要,但如果你不这样做,则必须修改条件以包括检查 laravel 文件夹中请求文档的任何内容。
猜你喜欢
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多