【问题标题】:Cannot remove public/index.php from url after changing configuration and .htaccess file更改配置和 .htaccess 文件后无法从 url 中删除 public/index.php
【发布时间】:2021-02-03 06:43:06
【问题描述】:

我现在正在本地服务器上工作,并试图从 url 中的 http://localhost/example/public/index.php/ 中删除“/public/index.php”。我已经关注了该站点的内容:https://www.tutsmake.com/how-to-remove-public-from-url-in-laravel/ 并添加了具有以下内容的配置文件 laravelex.conf:

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/example/public

    <Directory /var/www/example>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

在我的项目中,公共文件夹下我有.htaccess文件如下:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule> 

但是当我试图打开 http://localhost/laravel_project_folder/ 甚至 http://localhost/laravel_project_folder/index.php/ 我得到 内部服务器错误

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.
Apache/2.4.29 (Ubuntu) Server at localhost Port 80

日志文件中的错误是

AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

注意:我还尝试通过查看@stephen 的答案以以下方式更改 .htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

【问题讨论】:

  • 为什么不使用PHP artisan serve?
  • 因为在项目之间以及在实时服务器上切换时会出现问题。
  • 到底是什么问题?
  • 无法打开网址中没有“/public/index.php”链接的网站的任何页面

标签: laravel configuration apache2 php-7 laravel-5.8


【解决方案1】:

像这样改变文件结构:

├── project folder
│   ├── core
│   │   └── laravel files
│   ├── html
│   ├── css
│   └── index.php

将所有项目文件复制到core文件夹,将public文件夹的内容复制到主文件夹。

然后修复autoload.php和app.php在index.php中的路径:

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

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

【讨论】:

  • 这样可以完成,但我不想“将公用文件夹的内容复制到主文件夹”,因为它会删除项目的隐私。
  • 这种方式可能看起来很脏,但是当您想将其上传到服务器时,无论如何都必须这样做!此外,此方法不影响隐私。 @MItrajyotiKusari
【解决方案2】:

先抱歉回复晚了。但我发现我最终得到了来自不同来源的答案。首先,我按以下顺序放置我的项目文件:

├──.cpanel/
├── public_html/
    ├── .htaccess
    ├── index.php
    ├── web.config ...
├── public_ftp/
├── laravel_project_folder
│   ├── app
│   ├── bootstrap
│   ├── config ...
│   └── index.php

现在我在两个地方更改了文件内容。

  1. 在以下几行中更改了 index.php:

需要 DIR.'/../vendor/autoload.php'; 和 $app = require_once DIR.'/../bootstrap/app.php';

到

需要 DIR.'/../laravel_project_folder/vendor/autoload.php';和 $app = require_once DIR.'/../laravel_project_folder/bootstrap

/app.php';

  1. 也更改了 .htaccess 的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2017-09-28
    • 2011-05-20
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多