WordPress.org

Ready to get started?Download WordPress

Forums

Redirection
Is turning a 404 into a search a stupid thing to do? (7 posts)

  1. RoamFree
    Member
    Posted 1 year ago #

    I was able to use the Redirection plugin to redirect a 404 to a search for the missing page. So a search for http://example.com/pink-poodle becomes a search for pink-poodle on my site.

    Is this a stupid thing to do? At the moment I haven't thought of a context where it is a problem.

    (I'm avoiding saying how I did it... in case it is stupid, so I don't make it too easy for others to mess themselves up. And there is a side effect which requires me to always select from a pulldown in order to again create a normal redirect.)

    http://wordpress.org/extend/plugins/redirection/

  2. RoamFree
    Member
    Posted 1 year ago #

    Well, I found that the incantation which I put in Redirection's rules also has the side effect that the Redirection Log is empty. Apparently my rule which changes a 404 to a search is processed before the Redirection Log is processed -- there are no actual 404 pages shown, so there are no 404s to log.

    My site's Search plugin is showing the resulting increase in search activity in its statistics, so I can see that what should have been 404s are being processed.

  3. snurk
    Member
    Posted 1 year ago #

    I am also interested in doing this, if you protect yourself from SQL-injections it should be safe right?

    For me the redirection plugin did not catch all 404 errors. I made a simple function in 404.php that logs all 404 errors and gives me a notice on email.

    Would you mind sending me an email with how you did the search on [ redacted, support is offered via the forum and not email ]?

  4. RoamFree
    Member
    Posted 1 year ago #

    SQL injection is no more an issue than for any other URL.

    Nobody says this is stupid, so I'll go ahead and show what I did.

    Tools > Redirection
    Click on "Modules"... then add to the module "404" a group named "Change 404 to Search".
    This "group" is just one redirection rule.
    Source URL:/([^?]*)
    Target URL: /?s=$1
    Regex: Checked

    You want this to cause a Redirect... probably a 307 is the one to use.

    The rule takes whatever was given as the URL and uses it as the parameter for a site search. Because it is a redirect, the new URL goes back to the browser, who requests that page. The visitor sees the search result, which might have something relevant to the page which they were trying to reach.

    However, now nothing shows in the Redirection log. Apparently this redirection takes place before 404s are logged. And of course no 404s are being actually sent to client browsers, as they're always getting redirection status codes.

    I'm using the Relevantsi plugin for better searching, so now I look at its search statistics to see what people were searching for.

  5. snurk
    Member
    Posted 1 year ago #

    Sweet, this piece of code you wrote looks to me a bit like like htaccess mod rewrite rules, I'll try and make it work without plugin. Thank you :)

  6. RoamFree
    Member
    Posted 1 year ago #

    Yes, there are several ways to do it. It's just that the Redirection plugin is convenient for normal redirections, and this avoids having to manually maintain htaccess entries.

    It would be nice if this group's activity were logged, so the PLUS icon could be used to add relevant redirection rules. Right now I have to look at my search statistics to figure out what redir rules I should add.

  7. snurk
    Member
    Posted 1 year ago #

    I ended up with a different solution after some fidgeting around..

    In my 404.php I added this:

    $search_term = substr($_SERVER['REQUEST_URI'],1);
    $search_404 = str_replace('/','+',$search_term);
    $search_404 = str_replace('-','+',$search_404);
    $location = "http://www.mywebsite.se/?s=" . $search_404;
    session_start();
    $_SESSION['search_404'] = true;
    header("location: $location");

    And in my search.php I added this on top:

    session_start();
    $search_404 = $_SESSION['search_404'];
    session_destroy();

    And further down in search Php I added a notice:

    if($search_404){
    ?>
    <h1>Page could not be found(404)</h1>
    
    <h2>Perhaps you can find useful information in the articles below:</h2>
    <?php
    }

    Perhaps a bit messy but it works and without any involvement in htaccess or plugin.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic