we have an issue with the website (using Magento, on nginx) as it has ~5k URLs returning a 404 error. Most of these are product pages.

We need to make a 301 redirects for all these URLs, e.g.: redirect from www.example.com/category1/product.html to www.example.com/product.html.

What would be the best way to do that? Should we do this manualy for every page or is there a better way?

I would appreciate your help,

  • I've just found one method described in this article: bestresponsemedia.co.uk/…. I have doubts, because it talks about URL rewrite, not redirect. Can this method be used for redirection?
    – Vitalija
    Apr 5, 2016 at 6:18
  • Just use magento URL rewrites. You can make them redirects or rewrites and it's all handled inside magento
    – scrowler
    Aug 25, 2016 at 17:56

1 Answer 1


Nginx's url matching is able to do that, but for an entirely Magento solution, one could add an additional url lookup tool to the Magento error page. Put this in the '404 Not Found 1' CMS page:

{{block type="core/template" template="redirect/process.phtml"}}

Then create a redirect/process.phtml directory and file in your theme's template directory. Also add a list.csv file in the redirect directory, containing urls like:


Process.phtml would have something like:

$F = fopen(__DIR__.'/list.csv','r');
if ($F) while (($_ = fgetcsv($F))) {
  if ($_[0] == $_SERVER['REQUEST_URI']) {
    ->setRedirect($_[1], 301)

I assumed here that the contents of that CSV file are extremely large and not work I'd want to on every url lookup, or are changing faster than I'd want to keep updating Magento's internal redirect databasee. Example of that: https://gielberkers.com/how-to-programmatically-create-url-rewrites-in-magento-enterprise/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.