Laravel 7:外部 link 在本地主机中不工作

Laravel 7: External link not working in localhost

当我尝试通过 www.localhost/myproject 从本地主机访问我的 Laravel 项目时,CSS 和 JS 的外部链接不起作用。但其他人预计 运行 很好。我必须 运行 php artisan serve 命令才能完美地 运行 我的项目。当我将其上传到 Cpanel 托管时,它显示错误 This site can’t be reached。 这是 env 文件 APP_URL=http://localhost

中的应用 URL

这就是我声明链接的方式{{ asset('backend/mystyle.css') }} 我所有的 CSS, Js 文件都在 public 文件夹下。我认为 .htaccesss 中可能存在一些问题。

文件结构:

-app
-public
  --backend
  --css
  --robots.txt
  --web.config
-.env
-.htaccess
-index.php
-server.php

.htaccess 文件

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteBase /hrm-payroll-v3-2021/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/ [L]
    </IfModule>
    AddHandler application/x-httpd-php70 .php
   <IfModule mod_suphp.c>
     suPHP_ConfigPath /opt/php70/lib
   </IfModule>

Index.php 文件

<?php
 define('LARAVEL_START', microtime(true)); 
 require __DIR__.'/vendor/autoload.php';
 $app = require_once __DIR__.'/bootstrap/app.php';
 $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

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

 $response->send();

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

Server.php 文件

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/index.php';

你能告诉我为什么会出现这个问题吗?这个问题的可能解决方案是什么?

谢谢

您需要将 APP_URL 更改为您的域

如果您 运行 php artisan serve 不带任何参数,Laravel 会在端口 8000 上启动本地 Web 服务器。因此,您应该在 .env 文件中考虑这一点,方法是在 APP_URL.

中添加端口号

当您将项目上传到 cpanel 时,请确保网络服务器将 YourProject\public 引用为 document-root 目录。

我已经解决了更新 htaccess 文件的问题。

htaccess 文件:

    <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>
Options -MultiViews -Indexes

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_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images|trainer-cv)/(.*)$ public// [L,NC]

然后将 server.php 文件重命名为 index.php