Mobile app version of vmapp.org
Login or Join
Gail5422790

: Does it make sense to throw a 404 error with PHP? Problem Description Hi, The current setting of my .htaccess file does not allow any kind of 404 (as far as I know). I mean, it captures

@Gail5422790

Posted in: #301Redirect #Htaccess #Php #Seo

Problem Description

Hi,
The current setting of my .htaccess file does not allow any kind of 404 (as far as I know).

I mean, it captures every kind of url like mysite.com/asfasfasf then sends it to index.php?lang=asfasfasf then index.php 301-redirects to mysite.com/en.

Anyway,

Question

I have a gallery page which takes project name as an input, so if a nasty user comes and types mysite.com/gallery/asfasfasf, I should redirect them to my 404.php page, using php, but how?

I think my options are :


301-redirect to location 404.php
404-redirect to location '404.php'
Forget about 404.php, just print "project "asfasfasf" does not exist." on the gallery page.
??


Which one makes most sense? More importantly, which one is best for SEO ?

Thanks for any help!

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Gail5422790

2 Comments

Sorted by latest first Latest Oldest Best

 

@Angela700

Well the correct way in your case (which is: the user consciously requests an invalid resource) is to return a 404-response code. The body of the response is a description for the user of your site. If your 404.php is just like a template and contains mostly HTML-code then you could just include it. Otherwise showing the "project '$name' does not exist" is the best alternative.

So as an example, in PHP it would look like this (of course if you use a modern PHP framework then you can just use some kind of NotFound exception type):

if(project_exists($name)) {
// show the project like you do now
} else {
// project not found, return a 404 respose code
header('HTTP/1.0 404 Not Found');

// generate the response body either by
// including your 404-page (if it's mostly HTML, like a template)
include('/path/to/404.php');

// or just stating that the resource was not found
echo 'Project "', $name, '" does not exist!';
}


Regarding your options:


Number 1 doesn't make much sense - if a resource has never existed, then there is no point in redirecting (code 301 or 302) it to the home page or even a 404 page.

This only makes sense if the resource was deleted and you know what the new path is (for example if you have a map with old and new paths/project names) or if you can guess what the user wanted and redirect him to the proper resource (for example you could "fix" mistyped URLs with a fuzzy search). Of course you could also solve that with a "smart" 404 page.
Number 2 won't work. If I understand you correctly you would return a 404 response code and add a Location-header. But 4xx-codes must not return Location headers - only 3xx and 201 (and 202) response codes allow the Location header.


Even from a SEO perspective you should just use 404. First, there should be no links to invalid resources anyway and if you do something fancy here you hurt the people who actually use a link checker to check for invalid links on their side.

If the address of an existing resource changes, then it makes sense to use 301.

If the existing resource is just deleted (as in 410 Gone), then a redirect to a generic site (like the home page) may keep google happy, but it might not be user friendly (I hate it when I read an article about $TOPIC, click on a link in that article and get redirected to a total different page b.c. the link is wrong). You could use some light grey-hat stuff here like just redirect the Google/Bing/Yahoo bot and show 404 to the rest.

10% popularity Vote Up Vote Down


 

@Harper822

I know many developers have chose to simply process the link, if invalid load the main page and dispense with any feedback. From strictly and SEO perspective of search engine indexing and stuff, this won't hurt much because it will still resolve to a valid site and its yours.

However, from being a user friend perspective of giving your user feedback or perhaps for tracking how many invalid page accesses you had on your site (for security and access management) then you might want to have some kind of 404 in place. You can either create a sitewide rule that triggers or within your PHP code do an include to a 404 and upon doing validation the default fail would show that. Simple as that, you don't even need to redirect really.

The value of 404 throwing has been greatly debated among web developers but neither camp has managed to provide a de facto standard on that with a strong enough argument, so still comes down to individual developers to choose their camp.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme