skip to Main Content

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(IlluminateContractsHttpKernel::class);

 $response = $kernel->handle(
    $request = IlluminateHttpRequest::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

3

Answers


  1. Chosen as BEST ANSWER

    I have solve my problem with updating htaccess file.

    htaccess file:

        <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/$1/$2 [L,NC]
    

    then rename the server.php file to index.php


  2. you need to change your APP_URL to your domain

    Login or Signup to reply.
  3. 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 YourProjectpublic as document-root directory.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search