redirect 404 to similar urls

前端 未结 6 992
情深已故
情深已故 2021-01-30 11:16

I have a website with stories in it. I can have multiple types of stories within multiple categories like:

  • children
  • romance
  • scifi
  • action
6条回答
  •  梦如初夏
    2021-01-30 11:48

    Oh man, oh man!

    What you're asking for is not simple and need you to have a powerful computer, but the results are simply amazing.

    Here's what I'd suggest to do:

    • For 404 proper handling, you have the ErrorDocument redirection in vhost configuration. Mine looks like this: ErrorDocument 404 /404.php ;
    • When having a 404, Apache will call /404.php with all the arguments (which bad URL and so on, dump $_SERVER to see this). You have to test if there are only two expressions in the URL / i.e. http://mysite.com/(expr1)/(expr2)/
    • If not, then do a classical 404.
    • If yes then do a SOUNDEX search with MySQL (in your 404 Php file). See query sample here.
    • Then, in this "special" 404 case, do a suggestion, like google does, i.e.: "did you mean /action/story-name-action/? if so, click on the link".

    This a hard work, but it's both interesting and shows your skill. Very few websites do this (I just know google actually).

    Here's a demo on my French table that could give you an overview of how it works:

    mysql> SELECT * FROM job WHERE
    SOUNDEX( description ) LIKE SOUNDEX('Machiniste cinéma');
    +-------+--------------------+
    | id    | description        |
    +-------+--------------------+
    | 14018 | Machiniste cinéma  |
    +-------+--------------------+
    1 row in set (0.06 sec)
    
    mysql> SELECT * FROM job WHERE
    SOUNDEX( description ) LIKE SOUNDEX('Mchiniste cinéma');
    +-------+--------------------+
    | id    | description        |
    +-------+--------------------+
    | 14018 | Machiniste cinéma  |
    +-------+--------------------+
    1 row in set (0.06 sec)
    
    mysql> SELECT * FROM job WHERE
    SOUNDEX( description ) LIKE SOUNDEX('Machnste cinema');
    +-------+--------------------+
    | id    | description        |
    +-------+--------------------+
    | 14018 | Machiniste cinéma  |
    +-------+--------------------+
    1 row in set (0.06 sec)
    
    mysql> 
    

提交回复
热议问题