.htaccess 404 redirect - http-statuscode

Discussion corner for Developers of Serendipity.
bernd_d
Regular
Posts: 468
Joined: Thu Jun 03, 2010 9:28 am
Contact:

.htaccess 404 redirect - http-statuscode

Post 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.
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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}
=Don=
kleinerChemiker
Regular
Posts: 765
Joined: Tue Oct 17, 2006 2:36 pm
Location: Vienna/Austria
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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.
=Don=
bernd_d
Regular
Posts: 468
Joined: Thu Jun 03, 2010 9:28 am
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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.
onli
Regular
Posts: 2828
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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? :)
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bernd_d
Regular
Posts: 468
Joined: Thu Jun 03, 2010 9:28 am
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post by garvinhicking »

Hi!

Did you change the URL inside the plugin (check the preg_match) to match the URL of your staticpage?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bernd_d
Regular
Posts: 468
Joined: Thu Jun 03, 2010 9:28 am
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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 :(
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bernd_d
Regular
Posts: 468
Joined: Thu Jun 03, 2010 9:28 am
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post by bernd_d »

Hmmm yes, stupid mistake. I should have seen this by myself. :oops: 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 :shock:
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
bernd_d
Regular
Posts: 468
Joined: Thu Jun 03, 2010 9:28 am
Contact:

Re: .htaccess 404 redirect - http-statuscode

Post 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?
Post Reply