I have a wordpress website with dynamic content via API. every day we have hundreds of new content pages and hundreds of old pages that been deleted automatically. Is there a solution to create a automatic 301 redirect for this pages? The goal is to avoid thousands of 404 pages after a few weeks...
-
Meybe this solution will work for you too: https://stackoverflow.com/questions/8899805/edit-htaccess-with-php – ToTaTaRi Oct 12 '17 at 13:58
1 Answers
1a. Decide what you'll redirect TO.
Figuring where any particular OLD/missing url should be redirected to can be as simple [generic url] or complex [pattern-matching-language-AI] as you need. At the very least, you should probably try to match up similar articles by a few fields: url, title, meta, keywords, H-tags in content.
Redirecting to one common url IS a 404, whether you tell people it's a 301 or not.
Ultimately, if you're just going to redirect them all to one page, why bother with a redirect at all? What you've just built is a 404-page that will never 404, but will have the same effect on SEO. Instead, just customize your 404 page with that content.
1b. Categorize ALL pages. Redirect the Category page.
Setting some basic requirements for how your imported content will be organized will drastically simplify your problem. If you can lump these pages into some basic standard categories (which won't change, and need not be displayed in nav anywhere), your problem is 90% solved. The last step is then to simply create some simple rewrite rules from step 2a: url matches a category and page missing? 301 to category.
2a. Learn and apply your own rules with the Rewrite API.
This is not an uncommon problem, that's why there's a tool for it built-in to WP: the Rewrite API. This lets you add your own rewrite rules (matching Requested URL to specific Responses) to WP, to let WP do the rest.
I'm not going to tell you it will be easy, but it is the best solution, if you intended on proceeding despite my advice at the end.
2b. Catch-all requests, and wp_redirect before WP loads a 404.
Warning: here be dragons
Wordpress uses .htaccess to route most requests to index.php
, which itself acts as the main loader for all of WP. There are plenty of places in this process into which you could inject your own url-rewrite engine, effectively replacing wordpress for handling any of your predefined URLs. Once you're there, you can pretty much do anything with the request, but I'd suggest sticking to something simple, like wp_redirect( "http://example.com/new-page", 301 );
Please: Let your 404s be 404s, or only redirect to the page's old category.
There's a reason why this behavior is part of a well-defined standard for request/response cycles. Not just in HTTP, but this is one of 4 basic response states for ALL forms of communication responses: didn't-catch-that, my-answer, i-don't-know, choking. You ever worked with someone who never admits to themselves that they don't know something? That's what you'll be turning your site into.
Effectively, you're trying to save SEO for missing URLs, and to avoid massive amounts of 404 errors. To do it, at best you'll be directing old urls to completely unrelated content most of the time. This is effectively the same impact on SEO as showing a 404 page.
More on this at moz.com.

- 5,505
- 1
- 29
- 37