1

I want to optimize the link structure of an older Magento shop system. Until now, when generating the static pages, a .html suffix was added to the corresponding path. Due to an earlier problem, paths with double suffixes even existed (e.g. .html.html). Now that I have disabled the use of suffixes and deleted all paths with double suffixes from the database, I want to set up automatic redirections from the old URLs to the new ones.

To preserve existing hyperlinks and search engine entries, I want Nginx to redirect all requests for pages with a .html or .html.html suffix to the new path.

Requests to:

example.org/banana.html
example.org/banana.html.html

should be redirected to:

example.org/banana

so actual my best guess is:

location / {
  rewrite ^(.*?)\.html(\.html)?$ $1 permanent;
  try_files $uri $uri.html $uri.html.html $uri/ =404;
}

How do I reach my goal and what rewrite rules do I need to add to the nginx configuration?

max stern
  • 13
  • 3
  • The two `try_files` statements should be combined into a single statement, for example: `try_files $uri $uri.html $uri.html.html $uri/ =404;` – Richard Smith Apr 15 '20 at 06:05
  • Thanks a lot for your support! Are the rewrites a good solution to preserve the existing links to my site or are there other methods that are even more SEO friendly? – max stern Apr 15 '20 at 08:25

1 Answers1

0

Only use one try_files statement inside a location block. The file terms can be added as required. Include a $uri term to match those URIs which directly match static files (e.g. .css and .js). Include a $uri/ term if you want to use the index directive.

For example:

try_files $uri $uri.html $uri.html.html $uri/ =404;

See this document for details.


The rewrite statements are the wrong way around as the first one will always match a URI intended for the second one. Also, the rewrites are capturing a leading / in $1 variable, so by using /$1 you are adding an extra leading /.

You can also combine both regular expressions into a single statement, for example:

rewrite ^(.*?)\.html(\.html)?$ $1 permanent;
Richard Smith
  • 11,859
  • 2
  • 18
  • 26