Now the URLs for categories and products are like this:
- http://www.yourstore.com/Category/id-category-name.aspx
- http://www.yourstore.com/Product/id-product-name.aspx
But to optimize even more you can alter the settings and code in nopCommerce so you display the URLs even more nicely, like this:
- http://www.yourstore.com/categoryId-categoryName.aspx
- http://www.yourstore.com/categoryId-categoryName/productId-productName.aspx
You'll be asking why keep the id's for both categories and products. Because nopCommerce won't recognize the virtual path.
So the modifications are as follows:
1. In the admin area go to Configuration > Global settings > SEO/Display and set the Product and Category URL rewrite format as:
- product: {0}{1}-{2}/{3}-{4}.aspx
- category: {0}{1}-{2}.aspx
2. In the URLConfig.config from the nopCommerce web app modify the product and category rewrite rules like this:
<add name="ProductDetailsRewrite" virtualUrl="^~/([0-9]*)-([\w-]*)/([0-9]*)-([\w-]*)\.aspx(?:\?(.*))?"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/Product.aspx?ProductID=$3&$4"
ignoreCase="true" />
<add name="CategoryDetailsRewrite" virtualUrl="^~/([0-9]*)-([\w-]*)\.aspx(?:\?(.*))?"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/Category.aspx?CategoryID=$1&$2"
ignoreCase="true" />
*I don't know if the $4 and $2 from the destinationUrls are useless so I left them there. They are the product and category names
3. In code go to nopCommerce solution > Libraries > Nop.Common > SEO > SEOHelper.cs and replace the GetProductURL method with
public static string GetProductURL(int ProductID)
{
Product product = ProductManager.GetProductByID(ProductID);
if (product == null)
throw new NopException(string.Format("Product couldn't be loaded. ID={0}", ProductID));
string seName = GetSEName(product.SEName);
if (String.IsNullOrEmpty(seName))
{
seName = GetSEName(product.Name);
}
//jkr - seo optimization
//get the category and display it in the product url
//1. get the bottom most category - be sure to give the categories DisplayOrders don't leave them as "1"
Category category = CategoryManager.GetCategoryByID(product.ProductCategories[product.ProductCategories.Count - 1].CategoryID);
//2. if the category id and name are by some misterious ways null just display "Category" - haven't tested it so if it's not correct don't complain
int catId = 0;
string catName = "Category";
if (category != null)
{
//else save the id and name
catId = category.CategoryID;
catName = GetSEName(category.Name);
}
//so you don't get an error in the format
string url = string.Format(SettingManager.GetSettingValue("SEO.Product.UrlRewriteFormat"), CommonHelper.GetStoreLocation(false), category.CategoryID, catName, ProductID, seName);
return url;
}
If you have other SEO optimizations please let the community know about them :)