【问题标题】:Problem redirecting to /public folder in a Laravel project on Apache在 Apache 上的 Laravel 项目中重定向到 /public 文件夹时出现问题
【发布时间】:2021-10-17 11:03:47
【问题描述】:

我在 Laravel 项目中遇到以下情况。我将项目上传到 /var/www/html/tup 文件夹中的 Linux 服务器。

我可以使用链接http://www.example.com/tup/public 访问该站点,如图所示 (First Image)

我想直接访问而不在 URL i 中输入 public。 e. http://www.example.com/tup

我尝试在项目的主文件夹中使用 .htaccess 文件:

<IfModule mod_rewrite.c>
# That was ONLY to protect you from 500 errors
# if your server did not have mod_rewrite enabled

RewriteEngine On
# RewriteBase /
# NOT needed unless you're using mod_alias to redirect

RewriteCond %{REQUEST_URI} !/public
RewriteRule ^(.*)$ public/$1 [L]
# Direct all requests to /public folder

</IfModule>

但是当我进入http://www.example.com/tup时,Laravel 会抛出 404 Not Found 错误 (404 error Image)。我必须写 http://www.example.com/tup/index.php 让 Laravel 像 this image 一样重定向我。

好像没有检测到index.php文件

在公用文件夹中我有另一个 .htaccess 文件:

DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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

我已经修改了 apache2.conf 文件并将所有 AllowOverride 设置为 All 并启用了 mod_rewrite。

我也在apache2.conf中添加了这个配置:

<Directory /var/www/html/tup/public/>
    DirectoryIndex index.php
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

如果能提供任何帮助,我将不胜感激。

【问题讨论】:

  • 你重启服务器了吗? sudo systemctl apache2 restart
  • 是的,我重启了好几次。
  • 试试我在答案中写的

标签: php linux laravel apache debian


【解决方案1】:

你必须启用mod_rewrite:

sudo a2enmod rewrite
sudo service apache2 restart

在000-default.conf 中你应该有这样的东西:

<Directory /var/www/html/tup/public/>
    DirectoryIndex index.php
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    Require all granted
</Directory>

这会将您的网站(访问 http://www.example.com)重定向到 laravel 项目

【讨论】:

  • 正如我之前提到的 mod_rewrite 已经启用。我在 000-default.conf 中添加了这些行并重新启动了服务器,但仍然没有。
  • @Fdarfe 你想点击什么网址?什么 DNS 记录指向这个网络服务器?
【解决方案2】:

将以下内容添加到000-default.conf中,您将能够在没有任何重写规则的情况下访问。

    DocumentRoot /var/www/html/tup/public

    
    <Directory /var/www/html/tup/public>
        Options FollowSymLinks MultiViews 
        Require all granted
    </Directory>

【讨论】:

    猜你喜欢
    • 2017-08-02
    • 2020-10-31
    • 2019-04-10
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多