Support » Fixing WordPress » Redirect all to home except for some links

  • worldless

    (@worldless)


    I’d like to 301 redirect all 404 pages to homepage except for some links that I need to redirect them a specific location. with the following code I can redirect them all to homepage but how may I have an exception for individual links ?:

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".get_bloginfo('url'));
    exit();
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You can get the requested URI with $_SERVER['REQUEST_URI']. You could use that variable as part of a switch/case structure. In the case where $_SERVER[‘REQUEST_URI’] is == to a certain URI, redirect to the required location. For the default case (all other 404s) redirect per your current code.

    A switch/case structure could be replaced with a series of if/elseif/else statements if you find that more understandable. I’m assuming the 404 situation has already been established before the switch (or if()) statement, so that the code only executes when there is a 404. Otherwise you may need to use is_404() in an overall conditional block.

    worldless

    (@worldless)

    Thanks bcworkz
    But the problem is I’m not a programmer and I found the mentioned code surfing google. so I was hoping that someone could represent a simple conditional statement for that matter. I also learned that with the following code in .htaccess I can easily redirect any individual link:

    Redirect 301 /old-page/ http://www.website.com

    With all that said, isn’t there any simple conditional statement to redirect all 404s to homepage except for some specific links that I need to redirect them to a new address ?

    I wish there was a simple way !

    Moderator bcworkz

    (@bcworkz)

    Maybe the simplest (relatively speaking) approach would be to use .htaccess rules to redirect specific requests elsewhere, then for all other 404 requests that should go to home page are handled with that header code on your theme’s 404.php file.

    If the specific requests going elsewhere have some unique element in common, you could use RedirectMatch instead of Redirect. The /old-page/ parameter of RedirectMatch will be parsed as a regular expression (regexp), which is essentially wildcards on steroids. Let’s say a number of redirects all start with “/store”. You can redirect all such requests like so:
    RedirectMatch /store.*/+$ /new-store/
    The .*+$ chars all have special meaning in matching the correct URLs to redirect. .* matches anything until / is encountered, or the end of the URL. “/store-foo/” will be redirected, as would “/storefoobar/”. “/my-store/” will not be redirected with this rule. If there is no element in common, you’ve no choice but to list each URL individually.

    Requests that do not match any .htaccess rules get passed on to WP for evaluation. If WP cannot match the request to anything, it loads the theme’s 404.php template. If the very first code on the 404 template is the 301 and location header calls, all requests that 404 are redirected. Because of the exit; line, there is no point in having other content after that line.

    When you alter theme templates, to avoid losing your alterations when the theme updates, you should create a child theme. Obviously this complicates things, but it saves you from needing to reinstate your redirect code after every update. If you’d rather reinstate your code every time, that’s your decision. Either way works.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirect all to home except for some links’ is closed to new replies.