codehaven - php snippets - coding help
Generic filters
Exact matches only
  • Redirect WordPress if url contains a specific directory

    14th February 2020

    Wordpress - Miscellaneous

    wordpress category

    This can be a tricky one!

    Redirecting using .htaccess can be hit and miss at the best of times. If you want to redirect any url that contains a specific text without wordpress then that code snippet is here.
    But add to that the nature of WordPress and cache it can be a nightmare.

    But it seems I have found an effective way to redirect in WordPress.

    If my URL’s are like below and I wanted to redirect any URL that contained the directory /dogs/ it would mean redirecting each page manually, if you had 1000 of these it would take ages!

    http://aq.gthex.co.uk:10000/dogs/mycode/fun1
    http://aq.gthex.co.uk:10000/dogs/mycode/fun2
    http://aq.gthex.co.uk:10000/dogs/mycode/fun3

    In wordpress the normal redirection plugin does not seem to do what is needed. I tried the regex code but it failed each time.
    But what did work was the plugin SEO 301 Meta

    Any time a url contains /dogs/ it will be redirected to / (the base domain)

    seo redirect

    Was this code snippet helpful?