1 year ago
#288337
Alex
Assets found but not loaded after installing Laravel into a legacy project, possibly due to htaccess file not being loaded
I'm using this guide to install Laravel into a legacy application. I copied all legacy files into a single legacy
directory, installed a new Laravel 8 project (server doesn't use PHP8 yet so can't use L9) and copied the legacy
directory into the Laravel directory. I then followed the steps to make changes to the routes/web.php
file, created the LegacyController
and called the legacy application's index.php
file.
web.php;
Route::any('{path}', LegacyController::class)->where('path', '.*');
LegacyController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Response;
class LegacyController extends Controller
{
public function __invoke()
{
ob_start();
require app_path('Http') . '/legacy.php';
$output = ob_get_clean();
return new Response($output);
}
}
app\Http\legacy.php;
<?php
require __DIR__ . '/../../legacy/Website/index.php';
The legacy website uses a custom CMS which is loaded in its index.php
file. In short, it parses the URL to get the slug, according to which it looks up if a page exists with that slug. If so it looks up the component belonging to the page (components are basic PHP files) and loads them using include_once
. If a page isn't found, it redirects the user to /404
.
There are some .htaccess rules to allow for "friendly" URLs etc;
# Enable the rewrite engine
RewriteEngine On
RewriteBase /website.tld
# SSL Redirect 301 transfer the current request.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# never rewrite for existing files, and links
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# For Friendly URLs
RewriteRule ^ index.php [L]
However this .htaccess
file doesn't seem to be loaded at all, as existing files (for example, PDF files but also links to CSS and JS files) are all treated as having to be routed through the CMS and thus resulting in a redirect to the 404 page.
To test whether or not the .htaccess
file is loaded I added
ErrorDocument 200 "Hello. This is your .htaccess file talking."
RewriteRule ^ - [L,R=200]
to the file as described here, and as expected nothing happens. When I add the same lines to Laravel's .htaccess
file in the public
directory, I do get the message.
There are some other issues with other existing CMS pages not being found, but I feel like this all happens because the .htaccess
file is not being used.
How can I make sure the legacy application's .htaccess
file is properly used by Laravel?
php
laravel
.htaccess
legacy
0 Answers
Your Answer