1

This question already has an answer here:

I have a template file that 'gets' the product url so that I can produce a link to the product. However, the url called is not the SEO friendly version. Can anyone suggest an edit to the code below so that I can get the preferred SEO friendly URL instead?

Current URL

example.com/catalog/product/view/id/419/category/252

Preferred SEO Friendly URL

examples.com/my-product.html

PHTML - My Current Code

<?php $product = $categoryProducts->getFirstItem();?>
 <a href="<?php echo $product->getProductUrl()?>">Product Link</a>
<?php endif;?>

marked as duplicate by sv3n, Manoj Deswal, Prince Patel, Jai, liyakat Oct 17 '17 at 4:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

As per as your code,you have tried to get product($product) 's Seo Url from a product collection ($categoryProducts).

In order to get seo url you need to add addUrlRewrite() function to that collection ($categoryProducts).

So changes

<?php $product = $categoryProducts->getFirstItem();?>

to

<?php 
$categoryProducts->addUrlRewrite();
$product = $categoryProducts->getFirstItem();?>

Also,please check Magento indexing Management is working or not at system

Not the answer you're looking for? Browse other questions tagged or ask your own question.