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.



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 :

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

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

Thanks for any help!

2 Answers 2


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)
    // 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.

  • Thanks ! I totally agree you on hating deleted content :) And no, my links are valid, it's only for nasty users, well they could handle themselves I guess :) But I didn't understand your paragraph : > 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.
    – jeff
    Commented Sep 1, 2013 at 18:15
  • @CengizFrostclaw: thanks for your comment. What I basically tried to say is, that requests for not existing should fail properly. Sometimes it's even desired - like if you have some piece of content you want to remove from the search index. If the Google spider get's a 404 for an URL in the index, it will be removed automatically. Same with link checkers, if a link checker (which crawls your site for links) gets back 404 to a request, it can display that result properly to the user. Whereas if you return a 30x-code it might not show anything (depending on how the checker works).
    – vstm
    Commented Sep 4, 2013 at 18:45

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.