Answered by:
To prevent bad urls to your site

Question
-
User-1327394822 posted
i want to avoid people coming to my website like the below url
http://localhost:25446/Product/cake-model-1/dont-buy-this-cake
instead the correct and only url is
http://localhost:25446/Product/cake-model-1/
I did the below
var urldatacount = UrlData.Count; var productName = UrlData[0].Replace("-", " "); if(urldatacount >= 2){ Response.RedirectPermanent("~/Product/" + productName.Replace(" ", "-")); }
Is what I did good or are there any better ways?
Saturday, April 2, 2016 9:17 AM
Answers
-
User-821857111 posted
Is what I did good or are there any better ways?That's as good a way as any. There are other ways that you can reconstruct the proper URL, but they aren't "better".You should also add a <link rel="canonical"> tag to your pages to specify the actual URL for any page. That way. if there are multiple URLs pointing to the same resource (some of which may have been tampered with)) search engines will drop the incorrect ones.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 4, 2016 6:39 AM -
User-821857111 posted
I am actually constructing the correct url as your explained in your bookI'd forgotten all about that :) It was about 5 years ago that I wrote that.Where do I add the <link rel="canonical">You add it in the <head> section. I explain it in more detail here: SEO For ASP.NET Web Sites: URLs- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 4, 2016 8:01 AM
All replies
-
User-286291038 posted
Hi Gautam,
I think it will be better to show a 404 page not found error page instead of you arbitrarily removing the "-" in the URL and redirecting to a product page. What will happen if the product name is not present. Eg. the user enters, cake-modelss-34 and you code converts to cakemodelss34 which may not exist? If this happens regularly, it is even possible for your website SEO value to take a hit.
Also, this will cause the users to bookmark URLs which are actually not present in your website.
So, if you think users could try to access your products using "-" in the product name, better implement a redirect module which takes care of doing this redirection. Please refer to the below link which talks about it a bit,
http://alexwolfthoughts.com/efficient-mvc-redirects-using-an-http-module/
Saturday, April 2, 2016 2:29 PM -
User-821857111 posted
Is what I did good or are there any better ways?That's as good a way as any. There are other ways that you can reconstruct the proper URL, but they aren't "better".You should also add a <link rel="canonical"> tag to your pages to specify the actual URL for any page. That way. if there are multiple URLs pointing to the same resource (some of which may have been tampered with)) search engines will drop the incorrect ones.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 4, 2016 6:39 AM -
User-1327394822 posted
Hello Mike,
I am actually constructing the correct url as your explained in your book.. Below is the actual code.... I am checking the condition (urldata.count > 2) to avoid the url
mysite.com/Product/product-1/dont-buy-this-product
var productId = UrlData[0]; var db = Database.Open("xxx"); var sql = "Select * from Products where Id = @0"; var productDetails = db.QuerySingle(sql, productId); if(productDetails == null){ Response.Redirect("~/Collections/"); } if(UrlData.Count > 2){ var validUrl = Functions.SeoFriendly("~/Product", productDetails.Id, productDetails.Name); Response.RedirectPermanent(validUrl); } if(UrlData[1] != productDetails.Name.Replace(" ","-")){ var validUrl = Functions.SeoFriendly("~/Product", productDetails.Id, productDetails.Name); Response.RedirectPermanent(validUrl); }
Where do I add the <link rel="canonical">
Monday, April 4, 2016 7:38 AM -
User-821857111 posted
I am actually constructing the correct url as your explained in your bookI'd forgotten all about that :) It was about 5 years ago that I wrote that.Where do I add the <link rel="canonical">You add it in the <head> section. I explain it in more detail here: SEO For ASP.NET Web Sites: URLs- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 4, 2016 8:01 AM -
User-1327394822 posted
You add it in the <head> section. I explain it in more detail here: SEO For ASP.NET Web Sites: URLsHi Mike, I used it like below
@section canonical{ <link rel="canonical" href="~/Product/@productDetails.Id/@productDetails.Name" /> }
1) is the above correct?
2) to avoid a bad non existent url, i am checking urldata.count >2, it is ok to use such code?
Monday, April 4, 2016 9:19 AM -
User-821857111 posted
Hi Mike, I used it like below
@section canonical{ <link rel="canonical" href="~/Product/@productDetails.Id/@productDetails.Name" /> }
1) is the above correct?
@section canonical{ <link rel="canonical" href="@Functions.SeoFriendly("~/Product", productDetails.Id, productDetails.Name)" /> }
2) to avoid a bad non existent url, i am checking urldata.count >2, it is ok to use such code?Yes.
Monday, April 4, 2016 12:11 PM -
User-1327394822 posted
I thought you were replacing spaces with - in your URLs:I did this
<link rel="canonical" href="~/Product/@productDetails.Id/@productDetails.Name.Replace(" ", "-")" />
Monday, April 4, 2016 4:29 PM