A while back I made a mistake writing my urls and Google has them cached in old sitemaps. This is creating a ton of 404 errors in WMT that I need to clean up with a solid rewrite. I currently use the map module and rewrite each product individually.


/product1html/ /product1.html;

The mistake being in bold here: http://domain/productnamehtml/

The error here is I initially forgot the . before html and had an / at the end.

How can I rewrite the url to be http://domain/productname.html for every url that looks like /productnamehtml/.

  • Do you need only one particular url? – ALex_hha Mar 16 '16 at 15:40
  • No it needs to be a wildcard match for every product which I can't work out the regex for. – Kanyin Mar 16 '16 at 15:43

The rewrite will look like this:

rewrite ^(.*[^.])html/?$ $1.html permanent;

Or you might want to place it into a location:

location ~* ^(?<product>.*[^.])html/?$ {
    return 301 $product.html;

Explanation: Both regex are identical, except the latter has a named capture product. We look for any sequence that ends in a non-dot followed by html, followed by (optional) /. Everything before the html is captured.

| improve this answer | |
  • This answer fixed my issue right away. Now I need to look over the regex and understand why it works. Thanks for the help! – Kanyin Mar 16 '16 at 15:52
  • @Kanyin Added a short explanation.. – Richard Smith Mar 16 '16 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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