I'm migrating old website links to new website, it will use same domain
Here is some old links.
Old Links | New links |
---|---|
http://example.com/?p=contact | /contact |
http://example.com/?p=static&id=career | /career |
http://example.com/?p=static&id=about | /about |
http://example.com/?p=catalog&action=images&cat_id=1 | /product-category/category-slug-1 |
http://example.com/?p=catalog&action=images&cat_id=2 | /product-category/category-slug-2 |
http://example.com/?p=catalog&action=viewimages&pid=1&cat_id=1 | /product/product-slug-1 |
http://example.com/?p=catalog&action=viewimages&pid=2&cat_id=3 | /product/product-slug-2 |
The new product page doesn't have id in the URL, so I list all of them manually, and they are just 5 categories and 20 product pages in total.
This is what I've tried before I knew that nested if is not supported.
location / {
if ($arg_p = contact) { return 301 /contact; }
if ($arg_p = static) {
if ($arg_id = career) { return 301 /career; }
# other static pages redirect to /about
return 301 /about;
}
if ($arg_p = catalog) {
if ($arg_action = images) {
if ($arg_cat_id = 1) { return 301 /product-category/category-slug-1; }
if ($arg_cat_id = 2) { return 301 /product-category/category-slug-2; }
# other unlisted categories should redirect to /product-categories
return 301 /product-categories;
}
if ($arg_action = viewimages) {
if ($arg_pid = 1) { return 301 /product/product-slug-1/; }
if ($arg_pid = 2) { return 301 /product/product-slug-2/; }
}
# other unlisted links defaults to /products
return 301 /products;
}
}
What is the config should be?
Best Answer
You can do it with several chained
map
blocks. Here is an idea:Some
map
blocks can be shortened, for example, lets assume you have 3 static pages/career
,/clients
and "default" page/about
, 5 categories and 45 products:Update
OP states he can't use
map
directive since he doesn't have an access to full nginx config but only toserver
block contents. While previous solution is far more elegant (and should be more effective in terms of performance), it is possible to do the same using onlyif
blocks:This fragment can be placed either in
server
orlocation
context.