简体   繁体   中英

F3 routing engine works using XAMPP and PhpStorm on Linux, but gives 404 for CSS files

I'm trying to code a F3 ("Fat Free Framework") application using PhpStorm and XAMPP on Linux.

In order to make use of the .htaccess file provided by F3 necessary for the RewriteEngine (see the comments as why this is wrong), I launch my code using the following special run configuration :

从 PhpStorm 编辑运行/调试配置菜单

I launch the run configuration, which starts the Web Server. I open a browser and go to http://localhost:8000/ . I can see the content, and the links work and I can navigate from page to page, per the routes defined.

But none of the CSS is there. If I click on "view-source" and click on the CSS link, I get the 404 Not Found message from F3. So, it seems that for some reason, F3 is blocking CSS files.

The beginning of the webpage is as follow:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>A title</title>
    <link href="{{@BASE}}/f3style.css" rel="stylesheet" type="text/css" />
</head>

Although the @BASE variable is empty in my case. The "f3style.css" file is really at the root of my web directory currently.

I use the default .htaccess file recommended by F3:

 
 
 
 
  
  
  RewriteEngine On # Uncommenting the following line has no effect # RewriteBase / RewriteRule ^(app|tmp)\\/|\\.ini$ - [R=404] RewriteCond %{REQUEST_FILENAME} !-l RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L,QSA] RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
 
 
 

(Again, see the comments as to why the .htaccess file is irrelevant here)

And here are my routes. They are in "index.php":

<?php

$f3 = require('lib/base.php');

// Routes
require 'controller.php';
$f3->route('GET /index.php', 'Controller->showMain');
$f3->route('GET /', 'Controller->showMain' );
$f3->route('GET /items/@item', 'Controller->showItem');

$f3->run();

Any idea what I should look for?

Based on the screenshot you are using PHP's built-in web server . The thing is: it does not support .htaccess . Therefore all HTTP requests go to your router script ( framework/index.php -- part of the F3 app I guess).

I'm not familiar with how F3 routing works, but I'd assume it's the same as any other: it goes through all registered routes/endpoints and if no match found it will report "404 Not Found" response.

Since there are no rules from .htaccess in place to filter out requests for such static/existing files (css/jpg/png/js etc), the request goes through your index.php, through the whole routing table and it's F3 that reports that 404 error back to the browser (when no matching route was found).

There are a few possible solutions here:

  1. Create a separate route(s) for such static resources and serve them there from your F3 code (your code needs to locate a file and send a response with the right headers etc.)

    Not really worth implementing it this way TBH (since it's just to handle this specific situation / under PHP's web server).

  2. Do a pre-filter and ignore any requests to such static files. When using PHP's web server it's a matter of returning false in the router script (which tells PHP to use built-in web server code to serve such request). Look at the sample from the following link and add a similar one to your code: https://www.php.net/manual/en/features.commandline.webserver.php#example-426

     if (preg_match('/\\.(?:png|jpg|jpeg|gif|css)$/', $_SERVER["REQUEST_URI"])) return false;

    NOTE: you can add it right into the existing index.php like you did (at the top, after all use lines but before actual code lines) but this means that you are hardcoding such specific-to-PHP's-web-server-only logic into the script that can run under a proper web server.
    Instead I suggest creating a separate router script (eg php-router.php and use it instead of index.php from your screenshot) where you will do this and if execution will pass that condition, only then call the code from your real index.php (ie require './index.php'; or alike)


Why not use Apache web server from your XAMPP to serve the whole site in the first place?

  • Once Apache is running it handles ALL of the virtual hosts (that you can access via fake domain name in your OS' hosts file or by having website on a custom dedicated port) -- no need to launch each site separately.
  • Apache obviously fully supports .htaccess and always better/more features/more close to the production environment than for-dev-only PHP's built-in web server (mod_rewrite and other modules).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM