Php htaccess redirects to a boot file, prevents 404 errors with css files, etc.

I am working on creating my own structure and focus on learning more advanced programming methods than in college. I use .htaccess to redirect the entire page request to my boot file, which loads the controllers and themes based on the request URI.

URL format site.com/Controller[/optional/multiple/params]

The problem is, let's say I'm trying to include an image or css file, and it does not exist. In firebug they will not display as 404 errors due to redirection.

.htacess file

 Options +FollowSymLinks IndexIgnore */* # Turn on the RewriteEngine RewriteEngine On # Redirect to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php 

I check and parse the URIs and it forwards the 404 controller and generates a 404 header for the requested URI. However, for embedded files, such as CSS, images, etc., They show the same answer as the requested page, and will not generate 404 errors.

I.E. if you go to index.php, index.php will load fine, but all invalid css files will have the same header response as index.php.

Here is the code that the controller loads after parsing the URI:

  //locate $file = 'Lume/' . self::$AppPath . 'Controller/' . self::$ControllerName; if(!file_exists($file . '.php')){ header('HTTP/1.0 404 Not Found'); $file='Lume/' . self::$AppPath . 'Controller/_404.php'; } //create $controller = str_replace('/', '\\', '/' . $file); if(!is_subclass_of($controller, '\Lume\Abstracts\Controller')) throw new \Exception('Route::Main The controller must extend from Lume\Abstracts\Controller'); self::$Controller =& new $controller(); 
+4
source share
1 answer

Reading your .htaccess file, if the request is not for a file or directory that exists , then you request index.php to process the request. But you are not sending either query strings or how to pass data. Therefore, you must fix it. In this case, you can check whether the request was an invalid controller (you already know that this is an invalid file, because if you were not in index.php). Then you must header a 404 . Because, in this way, you will process both files not found, and contoller not found.

To fix this, you can check how other mvc frameworks do it.

Like for example codeigniter:

 RewriteCond $1 !^(index\.php) RewriteRule ^(.*)$ /index.php/$1 [L] 

What happens here, you have access to uri request as php variable. That way, you can perform checks inside your aka bootstrap front controller to see if the request is valid.

+1
source

All Articles