Forum

Thread tagged as: Problem, Configuration, Blog

News article page doesn't 404

I have a news article page with Clean URLS enabled in my .htaccess file. If I visit an article page with a URL that doesnt exist, it doesnt throw a 404, just a blank page. Ideally it would 404.

I tried doing a redirect using PHP:

header('Location: /page-not-found');
exit;

but I get an error: Warning: Cannot modify header information - headers already sent

.htaccess:

RewriteRule ^news-events/news/([a-zA-Z0-9-/]+)/preview$ /news-events/news/article.php?s=$1&preview=all [L]
RewriteRule ^news-events/news/([a-zA-Z0-9-/]+)$ /news-events/news/article.php?s=$1 [L]
Lance Cooper

Lance Cooper 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Where are you doing your redirect? Any headers need to be output before any HTML.

Can you check if the article page doesn't exist before the perch runtime, or html is output?

Im currently doing it like this:

<?php $html = perch_blog_post(perch_get('s'), true);
    if ($html) {
        echo $html;
    } else { ?>
        header('Location: /page-not-found');
        exit;
} ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

No, but you can do it after the runtime and before you output any HTML.

Ah ok, straight after the runtime works! Thanks very much for your help

What code did you end up with here?

I'm testing this, added after the Perch runtime

<?php $html = perch_blog_post(perch_get('s'), true);
    if ($html) {
//do nothing
    } else { header('Location: /page-not-found');
        exit;
} ?>

It's working but wanted to compare with what you have. Thanks.

Abby Larsen

Abby Larsen 0 points
Registered Developer

This discussion was really helpful to me, but I ended up in a slightly different place that worked and thought I would share.

I had a similiar problem where if I navigated to a non-existent page in the /blog subdirectory (e.g. https://mydomain.com/blog/mynonexistentpage) I didn't get the 404 page I spec'ed in my .htaccess file, but instead got the /blog/post.php page with no content except my global header and footer layouts.

I edited my /blog/post.php file such that my 404 page was included if $html didn't exist, otherwise post content was included as usual. It looks something like this and works great!

<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>

<?php 

    $html = perch_blog_post(perch_get('s'), true);
    if ($html) {

?>

// typical contents of post.php here

<?php

    } else { 

        header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
        include($_SERVER['DOCUMENT_ROOT'] . '/404.html');

    } 
?>

The one downside of this is that I’m actually managing the content of my 404 with Perch. 404.html is just a static snapshot of that Perch managed 404.php. When I included 404.php it kinda worked, but my 404.php content regions were undefined. Seems like there must be a way to pass my 404 content into 404.php with perch_content_custom, but my attempts failed.

This is what I tried in that else statement that didn't work, but perhaps folks will see what I’m trying to do here and have some ideas.

<?php

    } else {

    header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    perch_content_custom('Page Subtitle', array(
            'page'=>'/404.php'
    ));
    perch_content_custom('Page Content', array(
            'page'=>'/404.php'
    ));
    include($_SERVER['DOCUMENT_ROOT'] . '/404.php'); 
    // Half way there? Is there any way to get the 404.php content inserted into this include?

    } 
?>
Rachel Andrew

Rachel Andrew 394 points
Perch Support

Abby - if you need help please start a new thread rather than resurrecting a 7 month old one. Perch changes a lot in 7 months :) we also do not get notified if a solved thread is reopened by someone else so you are far less likely to get help.

Abby Larsen

Abby Larsen 0 points
Registered Developer

My apologies, Rachel — my intention wasn't to repoen this thread, but rather to append a solution that I had found for folks who might find this thread in the future. Maybe it was something better shared on the Slack chat? Sorry, I should have been more clear about this not being an official request for help from the support team. I shouldn't have muddied the waters with the "and one more thing I'm sitll wondering about" piece.