【问题标题】:Configuration of laravel under apache to avoid publicapache下laravel的配置避免public
【发布时间】:2015-10-28 00:43:42
【问题描述】:

我最近在 LAMP 服务器下安装了 laravel,但如果我没有在 url 中为 index.php 以外的其他路由指定 public,它就无法工作。

这是我的 apache 站点配置 (/etc/apache2/sites-available/furbook.com.conf):

<VirtualHost *:80>

ServerName furbook.calhost.com

DocumentRoot "/var/www/html/furbook.com/public"

<Directory "/var/www/html/furbook.com/public">

AllowOverride all

</Directory>
</VirtualHost>

然后从/etc/apache2/sites-enabled创建了一个符号链接: sudo ln -s ../sites-available/furbook.com.conf

最后,这是我在furbook/public 中的.htaccess 文件:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]

现在我可以跳过public 文件夹,localhost/furbook.com 按预期重定向到public/index.php

但问题是当我尝试路由像 localhost/furbook.com/cats 这样的 url 时,它会抛出一个找不到页面的错误。 如果我使用localhost/furbook.com/public/cats然后按预期工作。

我错过了什么?

【问题讨论】:

    标签: php apache .htaccess laravel


    【解决方案1】:

    没有足够的信息来 100% 权威地回答你的问题——这是我的最佳猜测。

    如果我使用 localhost/furbook.com/public/cats 然后按预期工作

    Apache 允许您在单个服务器上设置多个“虚拟主机”配置。 “VirtualHost”是一个域名,如furbook.calhost.com,它指向您计算机上的特定文件夹。之所以称为 Virtual,是因为在创建 Apache 时,强烈倾向于“一个主机名/一个物理服务器”

    <VirtualHost *:80>
    
    ServerName furbook.calhost.com
    
    DocumentRoot "/var/www/html/furbook.com/public"
    
    <Directory "/var/www/html/furbook.com/public">
    
    AllowOverride all
    
    </Directory>
    </VirtualHost>
    

    您已经为名称furbook.calhost.com

    设置了一个虚拟主机
    ServerName furbook.calhost.com
    

    但是,在您的 URL 中,您仍然通过名称 localhost 访问系统。通过localhost 访问意味着apache 将使用默认的主机/站点配置,该配置(似乎)仍然指向根目录上方的文件夹。

    您的选择

    1. 设置本地 DNS 或主机文件以使 furbook.calhost.com 指向 127.0.0.1

    2. 更改 apache 系统的 默认 配置(可能在 httpd.conf 中)以指向 public 文件夹

    祝你好运!

    (另外,不要忘记 Apache 系统通常需要重置才能获取新的配置更改)

    【讨论】:

    • 我选择了选项 1:编辑了我的 /etc/hosts 并添加了 127.0.0.1 furbook.com。非常感谢您的解释 :-)
    猜你喜欢
    • 2014-09-07
    • 2013-03-08
    • 2013-08-17
    • 2021-10-31
    • 1970-01-01
    • 2021-01-08
    • 2017-11-26
    • 2015-07-31
    • 1970-01-01
    相关资源
    最近更新 更多