Jump to content

cant generate 404 headers on pages other than the requested page


yamikowebs

Recommended Posts

I have been working on my own framework and learning more advanced PHP than what I was able to learn in college.

 

I use .htacess to redirect all page request to my bootstrap file.

The bootstrap file will then parse Request URI and run the necessary controllers etc.

If the controller does not exist it runs a 404 controller that send a 404 header.

After the controller and vies are made they are passed to a template and then echoed to the page.

 

If I request a faulty page I get the 404 error just fine. However If i request a valid page and the page request css files images that do not exist they will not generate the proper 404 header. They return the same header as the original page requested.

 

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

 

//the tid bit that loads the controller
//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();
//run
self::$Controller->Main();

Link to comment
Share on other sites

The above code was tested on my local server. I moved it to my live server on a test domain and got more information. On my live server I was getting an error about output already being sent from another file...This is confusing because the file that is starting the so called output is an abstract class and the line the error is for is an abstract function...

 

namespace Lume\Abstracts;
abstract class Chain{
abstract protected static function GetSelf();//this is the line that is starting output
abstract protected static function SetSelf();
}

Link to comment
Share on other sites

It returns 200 ok. In firebug on the html part of the response it has the same output as my main page though =/

 

This is my first time playing with the ideas of routing with htaccess and MVC so I might have redo the whole thing and start with a more abstract use of it to get the idea down.

 

Would you know of any good tutorials that go through setting 404s for invalid controllers. My URL format is site.com/controller/method/params/moreparams

Link to comment
Share on other sites

Those invalid files will have the same response as the request for Foo.php
So instead of base.css I get Foo.php again?  That doesn't make much sense.

 

Or do you mean you're getting a 200OK with no content?

 

So I started working on a simpler attempt to get the idea down and almost got it with

RewriteRule !.(js|ico|txt|gif|jpg|png|css)$ index.php [L,QSA]

 

I want everything to redirect to index.php except for actual files, directories or request for something in the Resoureces directory. That way I can pass image names with the extensions in the url if I wanted and generate proper 404 for requesting images, CSS files etc since they would be stored in the Resources directory.

 

I cant figure out how to get it to only prevent routing on Resources/*. I thought the code below would do it but something is wrong it is redirecting Resources/invalidfile to index.php

ReqriteRule !Resources/*$ index.php [L,QSA]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.