Divi WordPress Theme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix Reply To: Redirect all to home except for some links

Discussion in 'Misc WordPress Requests' started by bcworkz, Mar 31, 2018.

  1. bcworkz


    Reply To: Redirect all to home except for some links, by 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.

    Reply To: Redirect all to home except for some links

Share This Page

Monarch Social Sharing Plugin