Forum Moderators: bakedjake

Message Too Old, No Replies

Redirect on specific 404 pages to product category

         

jane_eyre

8:19 pm on Apr 11, 2019 (gmt 0)

5+ Year Member



I have this structure for cars https://example.com/2015-ford-focus-12345
So it is domain / year-make-model-id

I want to be able to redirect the user if they land on the page https://example.com/2015-ford-focus-12345 and the car no longer exists (404 error) to be redirected to https://example.com/inventory/?make=ford

I am trying to make it happen on nginx server. Please help, thank you!

I am familiar with .htaccess and apache but not nginx.

lammert

9:08 pm on Apr 11, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you plan to have multiple URLs to redirect, you may opt for a static redirect map. Create a configuration map.conf with the following contents:
map $request_uri $redirect_uri {
/2015-brand1-model1-n1 /inventory/?make=brand1
/2017-brand1-model2-n2 /inventory/?make=brand1
/2013-brand2-model3-n3 /inventory/?make=brand2
[...more fixed URL redirects...]
}

and add in your server configuration an include statement for the map file and a statement to execute the redirect:
include map.conf;
server {
[...your current server code...]
if ( $redirect_uri ) {
return 301 $redirect_uri;
}
}

lucy24

9:11 pm on Apr 11, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you’re looking for is the nginx equivalent of a -f check: take this action if the requested file doesn’t exist.

But wait!

#1 No matter what your server type, this is a fairly server-intensive approach, since it then has to check twice on the same request. It would be a lot tidier if you could do the checking yourself--which is probably feasible, since you must have taken some action to delete the pages, so you know which ones they are.

#2 Be cautious about handling any type of 404 with a redirect. What you might think about doing instead is returning a 410, and spend some time developing a really useful 410 page.