Home > Net >  Laravel 7: External link not working in localhost
Laravel 7: External link not working in localhost

Time:05-11

The external links of CSS and JS are not working when I try access my Laravel project from localhost by www.localhost/myproject this. But others porject run well. I have to run php artisan serve command to run my project perfectly. When I upload it to Cpanel hosting it shows the error This site can’t be reached. This is the app URL in env file APP_URL=http://localhost

This is how I declare the links {{ asset('backend/mystyle.css') }} All of my CSS, Js files are under public folder. I think there might some problem in .htaccesss.

File structure:

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

.htaccess file

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

Index.php file

<?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 file

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

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

require_once __DIR__.'/index.php';

Can you please tell me why this problem may occur? What is the possible solution to this problem?

Thanks

CodePudding user response:

you need to change your APP_URL to your domain

CodePudding user response:

If you run php artisan serve without any arguments, Laravel starts a local web server on port 8000. Therefore you should consider this in your .env file by adding the port number in your APP_URL.

When you upload your project to a cpanel, make sure the web server is referring to YourProject\public as document-root directory.

  • Related