Forum

Thread tagged as: Error, Blog

404 for blog

I'm using .htaccess to set my 404 page. I'm also rewriting blog paths in the same .htaccess file. The 404 page comes up just fine for the following:

http://copyanddesign.com/wrongpage - > goes to http://copyanddesign.com/404.php as intended http://copyanddesign.com/blog/wrongpage.php -> works as well

Here's where it breaks down: http://copyanddesign.com/blog/wrongpage (without .php at the end) - > gives me a page with this error: "Warning: Invalid argument supplied for foreach() in /nfs/c05/h03/mnt/70481/domains/copyanddesign.com/html/blog/post.php on line 58"

Line 58 is " foreach($categories as $cat) {" from the code for related posts in post.php.

My .htaccess:

ErrorDocument 404 http://copyanddesign.com/404.php RewriteEngine on RewriteRule ^blog/([a-zA-Z0-9-/]+)/preview$ /blog/post.php?s=$1&preview=all [L] RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]

Is there a workaround here to prevent that error from coming up?

Thanks!

Franz Neumann

Franz Neumann 0 points

  • 2 years ago
Drew McLellan

Drew McLellan 2636 points
Perch Support

Does the URL work without rewriting?

If I disable rewriting in .htaccess, then yes, http://copyanddesign.com/blog/wrongpage goes to the 404.php page.

Drew McLellan

Drew McLellan 2636 points
Perch Support

Ok. What part makes this a Perch question rather than a general URL rewriting question?

Well, it's not a Perch-code-related question, no, but it is an issue that arises, at least for me, if the url points to a blog post that doesn't exist (or, say, is typed in incorrectly or comes from a bad link.)

So when I follow the advice here: https://docs.grabaperch.com/video/v/simple-url-rewriting/ but then tweak the url of a blog post to be something that doesn't exist, I get: Warning: Invalid argument supplied for foreach() in /nfs/c05/h03/mnt/70481/domains/copyanddesign.com/html/blog/post.php on line 58

Going to: http://copyanddesign.com/blog/project-dashboards-wrong-url for example will display that error.

Sure, it's an edge-case and probably won't happen often, but I'd like to avoid the possibility of visitors seeing error messages. Simply posting to see if anyone has any insight on how to resolve this, that's all.