"Back to category" button for product belonging to multiple categories
I'm working on an e-commerce website and I'm faced with this issue: I need to place a "Back to category" button on my product pages but some of these products are linked to 2 or 3 different categories. I don't know if I just have to choose the most relevant category, or if I should create a button that just takes the user back to the previous page.
For example: In the first case, if a user comes from Category 1 and the button is linked to Category 2, they will be taken to Category 2.
In the second case, the problem is that if a user comes from an external page, they would be carried out of the website.
What is the best solution for this situation?