Forum

Thread tagged as: Problem, Configuration

Missing page throws a 500 error instead of 404 on pages now found.

Here's whats in my .htaccess file

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ $1.php [L,QSA]

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/perch RewriteRule ^([a-zA-Z0-9-/]+)$ /post.php?s=$1 [L] </IfModule>

secure htaccess file

<Files .htaccess> order allow,deny deny from all </Files>

Any ideas why it's not throwing a 404 error on pages not found?

Andrew Areoff

Andrew Areoff 0 points

  • 6 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

What is in your error log? A "500 error" is an error notification.

I've managed to get rid of the 500 error but now if I go to a page that doesn't exist it just goes to another page rather than throwing a 404 error.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Your rewrite rule is marching almost anything and routing it to /post.php

This is a rewrite rule you recommed for blog post clean URLs:

RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]

I want my blog posts to be in the root of the site. How do I make that work without using this:

RewriteRule ^([a-zA-Z0-9-/]+)$ /post.php?s=$1 [L]

Drew McLellan

Drew McLellan 2638 points
Perch Support

Therein lies the challenge. I think the only way you can tell a valid post slug from an invalid 404 is to see if you have a matching post. So it would be better to handle the 404 within post.php than trying to deal with it in your rewrite rules.

Ah ok.

How would I do that within post.php?

Otherwise I'll have to live with putting the blogs into a folder – it's not the end of the world.

Drew McLellan

Drew McLellan 2638 points
Perch Support

$post = perch_blog_custom([
    'filter' => 'postSlug',
    'value' => perch_get('s'),
    'skip-template' => true,
]);

if (count($post)==0) {
    // No matching post!
    include('404.php');
    exit;
}

Ok this is what I've ended up with:

            <?php perch_blog_post(perch_get('s')); ?>

<?php perch_blog_custom(array( 'filter' => 'postSlug', 'value' => perch_get('s'), 'skip-template' => true, ));

if (count($post)==0) { // No matching post! include('includes/404.php'); exit; } ?>

But this then includes the contents of my 404. page at the end of the post.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You probably want to test for the 404 first.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You probably want to test for the 404 first.

Yep, Im getting the 404 page as expected if there's a page that doesn't exist so that part of the problem is fixed.

It's just that the include is printing underneath the contents of the post.php page and stopping the rest of the page displaying from there.

Drew McLellan

Drew McLellan 2638 points
Perch Support

So put it first - right at the top after the runtime include.

That doesn't work. It kills the whole page then.

I've put the blog posts into a folder as that solves the problem more effectively.

Drew McLellan

Drew McLellan 2638 points
Perch Support

It kills the whole page then.

What error are you getting?

no error. Just a blank page.

I've put the blog section into a folder now and that seems a better solution in hindsight I think.