【问题标题】:Laravel 6.4 index.php URL rewriting .htaccess errorLaravel 6.4 index.php URL重写.htaccess错误
【发布时间】:2019-11-03 23:31:16
【问题描述】:

这不是关于如何使用.htaccess 文件类型的问题删除index.php 的问题。

我在我的本地 Apache 服务器中设置了一个 Laravel 6.4 应用程序。我已将我的公共目录移动到 /var/www/html 并编辑了默认 vhost 文件以在输入 http://localhost 时加载它。

我将默认vhost改为指向/var/www/html/public目录的原因是,当从局域网中的另一台计算机调用ip地址时,我需要加载Laravel应用程序。

当我输入http://localhost 时,它工作正常,并且显示右上角带有Login 和Register 链接的默认Laravel 欢迎页面。

但是当我导航到 http://localhost/login 或 http://localhost/register 时,它会出现 404 错误。 然后当我输入http://localhost/index.php/login 或http://localhost/index.php/register 时,页面加载正常,没有任何问题。

我尝试使用在 Internet 上找到的参考资料对 .htaccess 文件进行更改,但到目前为止没有成功。

以下是我目前的设置。

Laravel 应用程序文件被放在名为 testapp 的目录中 public 目录之前的级别中,我已修改 index.php 文件以指向该目录。

有关详细信息,请参阅下面的控制台输出。

我的操作系统版本是 Ubuntu 19.04。

下面是/var/www/html里面的目录结构。

isuru@isuru-aspire-e5-574g /var/www/html $ ls -al
total 24
drwxr-xr-x  5 root  root     4096 Nov  3 02:34 .
drwxr-xr-x  3 root  root     4096 Nov  2 16:18 ..
-rw-r--r--  1 root  root      168 Jun  5 06:29 index.html
drwxr-xr-x 13 isuru www-data 4096 Aug  9 13:26 phpmyadmin
drwxr-xr-x  5 isuru www-data 4096 Nov  3 12:41 public
drwxr-xr-x 13 isuru www-data 4096 Nov  3 02:34 testapp

以下是 testapp 和 public 文件夹中的目录结构。

testapp 文件夹

isuru@isuru-aspire-e5-574g /var/www/html/testapp $ ls -al
total 712
drwxr-xr-x  13 isuru www-data   4096 Nov  3 02:34 .
drwxr-xr-x   5 root  root       4096 Nov  3 02:34 ..
drwxr-xr-x   6 isuru www-data   4096 Nov  3 02:34 app
-rw-r--r--   1 isuru www-data   1686 Nov  3 02:34 artisan
drwxr-xr-x   3 isuru www-data   4096 Nov  3 02:34 bootstrap
-rw-r--r--   1 isuru www-data   1531 Nov  3 02:34 composer.json
-rw-r--r--   1 isuru www-data 178175 Nov  3 02:34 composer.lock
drwxr-xr-x   2 isuru www-data   4096 Nov  3 02:34 config
drwxr-xr-x   5 isuru www-data   4096 Nov  3 02:34 database
-rw-r--r--   1 isuru www-data    220 Nov  3 02:34 .editorconfig
-rw-r--r--   1 isuru www-data    777 Nov  3 13:01 .env
-rw-r--r--   1 isuru www-data    726 Nov  3 02:34 .env.example
-rw-r--r--   1 isuru www-data    111 Nov  3 02:34 .gitattributes
-rw-r--r--   1 isuru www-data    163 Nov  3 02:34 .gitignore
drwxr-xr-x 703 isuru www-data  20480 Nov  3 02:34 node_modules
-rw-r--r--   1 isuru www-data   1144 Nov  3 02:34 package.json
-rw-r--r--   1 isuru www-data 429975 Nov  3 02:34 package-lock.json
-rw-r--r--   1 isuru www-data   1297 Nov  3 02:34 phpunit.xml
drwxr-xr-x   4 isuru www-data   4096 Nov  3 02:34 public
drwxr-xr-x   6 isuru www-data   4096 Nov  3 02:34 resources
drwxr-xr-x   2 isuru www-data   4096 Nov  3 02:34 routes
-rw-r--r--   1 isuru www-data    563 Nov  3 02:34 server.php
drwxrwxr-x   5 isuru www-data   4096 Nov  3 02:34 storage
-rw-r--r--   1 isuru www-data    174 Nov  3 02:34 .styleci.yml
drwxr-xr-x   4 isuru www-data   4096 Nov  3 02:34 tests
drwxr-xr-x  41 isuru www-data   4096 Nov  3 02:35 vendor
-rw-r--r--   1 isuru www-data    538 Nov  3 02:34 webpack.mix.js

public 文件夹

isuru@isuru-aspire-e5-574g /var/www/html/public $ ls -al
total 36
drwxr-xr-x 5 isuru www-data 4096 Nov  3 12:41 .
drwxr-xr-x 5 root  root     4096 Nov  3 02:34 ..
-rw-r--r-- 1 isuru www-data    0 Nov  3 01:39 access.log
drwxr-xr-x 2 isuru www-data 4096 Nov  3 02:35 css
-rw-r--r-- 1 isuru www-data    0 Nov  3 01:39 error.log
-rw-r--r-- 1 isuru www-data    0 Nov  3 02:35 favicon.ico
-rw-r--r-- 1 isuru www-data  593 Nov  3 12:41 .htaccess
-rw-r--r-- 1 isuru www-data 1839 Nov  3 11:59 index.php
drwxr-xr-x 2 isuru www-data 4096 Nov  3 02:35 js
-rw-r--r-- 1 isuru www-data   71 Nov  3 02:35 mix-manifest.json
drwxr-xr-x 4 isuru www-data 4096 Nov  3 02:35 public
-rw-r--r-- 1 isuru www-data   24 Nov  3 02:35 robots.txt
lrwxrwxrwx 1 isuru www-data   40 Nov  3 12:39 storage -> /var/www/html/testapp/storage/app/public

以下是我的.htaccess 文件和index.php 文件的内容。

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

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


<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../testapp/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../testapp/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

请注意,直接创建一个虚拟主机到公用文件夹工作得很好,当我在我的生产服务器(共享)中设置它时,它也工作得很好。当我在生产服务器(共享)中设置 Laravel 应用程序时,我已经完全复制了我正在做的事情,除了将默认 vhost 文件更改为公共文件。

请向我提供解决此问题的任何说明。如果我可以强制 index.php 到每个 URL,它也适用于我。提前感谢任何人的帮助。

【问题讨论】:

  • 已启用mod_rewrite(检查a2query -m rewrite)
  • 为什么需要将公用文件夹移动到/var/www/html?我相信您只需将默认虚拟主机指向/var/www/html/testapp/public..
  • @apokryfos 是的,它已启用。
  • @nmfzone 是的,我也可以。

标签: php laravel .htaccess laravel-6


【解决方案1】:

根据互联网上的发现,我已经解决了我的问题。

问题是将 /etc/apache2/apache2.conf 设置为 AllowOverride all to /var/www/html 和 /usr/share 目录。

现在我的 apache2.conf 看起来像这样。

.... content above

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride all
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride all
    Require all granted
</Directory>

#<Directory /srv/>
#   Options Indexes FollowSymLinks
#   AllowOverride None
#   Require all granted
#</Directory>


.... content below

感谢所有评论的人。这一直很容易解决。

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多