Page 1 of 2
.htaccess 404 redirect - http-statuscode
Posted: Thu Aug 19, 2010 10:15 am
by bernd_d
I have tried to make a static page called 404 with a little content like "sorry, page not found" to have same style as for my blog-site.
Within .htacces i have included
Code: Select all
ErrorDocument 404 /index.php?serendipity[subpage]=404
Redirect works good if url is not found, but http-code is returned with 200 instead of 404.
If i redirect to index.php without arguments, http-code 404 is send.
Is it possible to change this behavior? I don't want to create an external page which is in another design than my page and with my wished solution the visitor could use search function directly.
Re: .htaccess 404 redirect - http-statuscode
Posted: Thu Aug 19, 2010 9:55 pm
by Don Chambers
You could try this in your template's index.tpl file:
Code: Select all
{if $view=='404'}
<div class="serendipity_Entry_Date">
<p>Sorry, page not found.</p>
</div>
{else}
{$CONTENT}
{/if}
Re: .htaccess 404 redirect - http-statuscode
Posted: Fri Aug 20, 2010 8:57 am
by kleinerChemiker
But that won't change the http-status code. The problem is, the script itself (->s9y) has to emmit the right status code.
Re: .htaccess 404 redirect - http-statuscode
Posted: Fri Aug 20, 2010 11:16 am
by garvinhicking
Hi!
Since you show a proper "subpage" for that, the code will always be 200, because it's a valid page. You would need to patch/write your own event plugin to create such a page, so that you can emit your 404 header...
Instead, I'd maybe write a .htm file for 404 responses? Then the 404 won't get changed.
Regards,
Garvin
Re: .htaccess 404 redirect - http-statuscode
Posted: Fri Aug 20, 2010 6:06 pm
by Don Chambers
kleinerChemiker wrote:But that won't change the http-status code. The problem is, the script itself (->s9y) has to emmit the right status code.
Good point. My suggestion is only for a friendly, human readable "not found" page that looks like the rest of the template.
Re: .htaccess 404 redirect - http-statuscode
Posted: Mon Aug 23, 2010 9:57 am
by bernd_d
garvinhicking wrote:Since you show a proper "subpage" for that, the code will always be 200, because it's a valid page.
Wouldn't it be possible to implement some code into core, that includes a 404.php or something like this from template-folder into content, if html-status is 404?
garvinhicking wrote:You would need to patch/write your own event plugin to create such a page, so that you can emit your 404 header...
I would, if i could
garvinhicking wrote:Instead, I'd maybe write a .htm file for 404 responses? Then the 404 won't get changed.
That's what i do at the moment, but it is not very user-friendly. The page has no tags, no sidebar, no design and it doesn't have a search-field.
Re: .htaccess 404 redirect - http-statuscode
Posted: Tue Aug 24, 2010 12:31 am
by onli
But it's possible to include all of this, well, maybe without the tabs if they change often. Just grab the html from your index.html, remove the entries and everything too dynamic.
Re: .htaccess 404 redirect - http-statuscode
Posted: Tue Aug 24, 2010 9:40 am
by garvinhicking
Hi!
Wouldn't it be possible to implement some code into core, that includes a 404.php or something like this from template-folder into content, if html-status is 404?
No, ther core's not a good place for this.
A simple event plugin would look like this:
Code: Select all
<?php
class serendipity_event_404hook extends serendipity_event {
function introspect(&$propbag) {
global $serendipity;
$propbag->add('name', '404 hook');
$propbag->add('description', '');
$propbag->add('requirements', array('serendipity' => '1.5', 'smarty' => '2.6.7', 'php' => '4.1.0'));
$propbag->add('version', '0.1');
$propbag->add('author', 'Garvin Hicking');
$propbag->add('event_hooks', array('entries_header' => true));
}
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
if ($event == 'entries_header' && preg_match('@/pages/error.html@i', $_SERVER['REQUEST_URI'])) {
serendipity_header('HTTP/1.0 404');
serendipity_header('Status: 404 Not Found');
}
return true;
}
}
That's not too hard, right?

Re: .htaccess 404 redirect - http-statuscode
Posted: Tue Aug 24, 2010 12:39 pm
by bernd_d
Thank you Garvin!
I have taken your code-example and installed it as an plugin.
It is shown within event-plugin-section too.
If i call a non existing page, my static 404-page is shown, but html-status still is 200
Where is my mistake?
Re: .htaccess 404 redirect - http-statuscode
Posted: Tue Aug 24, 2010 2:36 pm
by garvinhicking
Hi!
Did you change the URL inside the plugin (check the preg_match) to match the URL of your staticpage?
Regards,
Garvin
Re: .htaccess 404 redirect - http-statuscode
Posted: Thu Aug 26, 2010 8:20 am
by bernd_d
OK, here we go again...
.htaccess
Code: Select all
ErrorDocument 404 /index.php?serendipity[subpage]=404
serendipity_event_404hook.php
Code: Select all
<?php
class serendipity_event_404hook extends serendipity_event {
function introspect(&$propbag) {
global $serendipity;
$propbag->add('name', '404 hook');
$propbag->add('description', '');
$propbag->add('requirements', array('serendipity' => '1.5', 'smarty' => '2.6.7', 'php' => '4.1.0'));
$propbag->add('version', '0.1');
$propbag->add('author', 'Garvin Hicking');
$propbag->add('event_hooks', array('entries_header' => true));
}
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
if ($event == 'entries_header' && preg_match('@/index.php?serendipity[subpage]=404@i', $_SERVER['REQUEST_URI'])) {
serendipity_header('HTTP/1.0 404');
serendipity_header('Status: 404 Not Found');
}
return true;
}
}
Plug-in is the first one of event-plug-ins, don't know if it has to be first or last. Seems to make no difference for http-statuscode.
If i call
http://blog.example.org/thispagedoesnotexist, my static 404-page is shown, but i get
Status 200 OK. I don't know, where the problem could be

Re: .htaccess 404 redirect - http-statuscode
Posted: Thu Aug 26, 2010 11:07 am
by garvinhicking
Hi!
Ah, okay. I was forgetting you did not use URL rewriting for a /pages/error.html kind of page.
"?" is a special character, so the regular expression would need to look more like this:
Code: Select all
if ($event == 'entries_header' && preg_match('@index.php\?serendipity\[subpage\]=404@i', $_SERVER['REQUEST_URI'])) {
HTH,
Garvin
Re: .htaccess 404 redirect - http-statuscode
Posted: Thu Aug 26, 2010 11:28 am
by bernd_d
Hmmm yes, stupid mistake. I should have seen this by myself.

But still not working
If i call
http://blog.example.org/adsfdsfasjkdfadsfasd, 404-page is shown, but i get status-code 200
If i call
http://blog.example.org/pages/404.html, 404-page is shown, but i get status-code 200
If i call
http://blog.example.org/index.php?seren ... bpage]=404, 404-page is shown and i get status-code 404

Re: .htaccess 404 redirect - http-statuscode
Posted: Thu Aug 26, 2010 11:45 am
by garvinhicking
Hi!
Hm, okay. I think the problem is that the ErrorDocument handler keeps $_SERVER['REQUEST_URI'] as the original one, instead of the redirected one.
We could try to fix it by checking the $_SERVER if there's some kind of "REDIRECT_REQUEST_URI" or whatever. But maybe even better the solution could be to use this:
Code: Select all
if ($event == 'entries_header' && $GLOBALS['serendipity']['subpage'] == '404') {
Instead of relying on the URL, this should detect the actual s9y subpage being displayed.
Regards,
Garvin
Re: .htaccess 404 redirect - http-statuscode
Posted: Thu Aug 26, 2010 12:04 pm
by bernd_d
Now it shows 200 for all cases again
Update: I have put
Code: Select all
echo $GLOBALS['serendipity']['subpage'];
into index.php, but i never geht an output?!? Shouldn't there be something like 404 or contactform, if i am on this static-pages?