Forum
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:
https://copyanddesign.com/wrongpage - > goes to https://copyanddesign.com/404.php as intended https://copyanddesign.com/blog/wrongpage.php -> works as well
Here's where it breaks down: https://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 https://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!
Does the URL work without rewriting?
If I disable rewriting in .htaccess, then yes, https://copyanddesign.com/blog/wrongpage goes to the 404.php page.
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: https://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.