I am trying to add a link on my website's product pages and redirect the users based on the category that the product belongs to. For example, I have a category chairs which is having a Category Id of 39 and I am having another category desks for which the category id is 52.
Now my issue with the above scenario is when I am moving into the product page through category pages I am getting the above-mentioned category Id's properly and the URL will be
but when I am trying to search for the product directly from the search box and move into the product page from there the URL becomes
the category id becomes 2 and this happens with every category and it shows the category name as default category when I go to the product page through search box. How can I resolve this? kindly let me know if there is any parameter that doesn't change on the product page which can enable me to display the content based on the category the product belongs to.