根据指定的组(EE)将“单词”(类别)插入URL

I am using php routing to grab the related category assigned to an entry (in Expression Engine CMS) and insert it into the relevant place in the url. What i have below works well, however i need to adjust this code so that it accepts multiple category names. Currently it only does this for 'beauty', but I also need it do do the same thing for 4 others: hair, nails, equipment, furniture.

I am no php expert so my attempts to adjust this have failed. Any help is appreciated!

{exp:search:search_results}

<?php $autoPath = '{auto_path}';
$lastSegment = basename($autoPath);

//Beauty URL update, based on the category assigned to the entry

if(strpos($autoPath, 'beauty')){

//variable to assign the category to the appropriate URL segment for the Rule

$catVar = 1;
$basename = 'catPath';

?>

{categories}
 <?php
 $tempname = $basename.$catVar;
 $$tempname = "{category_url_title}";
 $catVar++;
 ?>
{/categories}

<?php $autoPath = "http://www.my.site/products/"."beauty/$catPath1/$lastSegment";
} ?>

<h3><a href="<?php echo $autoPath; ?>" title="{title}">{title}</a></h3>

To explain further.. The page url's to each product appear like so:

site.com/product-range/beauty/brand/beautyproduct1

site.com/product-range/hair/brand/hairproduct1

Beauty, Hair, Nails, etc. are 'Categories'. Within each of these categories are all the related brands.

The href to a product (entry in the cms) is like so: href="/beauty/{categories}{category_url_title}{/categories}/{url_title}"

Now with the Search funtion, to produce results you use

<a href="{auto_path}">{title}</a>

What that produces is: site.com/product-range/beauty/beautyproduct1

..which leaves out the 'brand' since all brands are 'categories', and are not inserted automatically into the url. What the code above does is see what category is assigned to the product - in this case 'Beauty' - and then adds the related 'brand' into the required spot in the url.

So i just need the code updated so that it produces the same result but includes a few other categories (Hair, Nails, Equipment, Furniture).