0

I have an ASP.net MVC 5 site. The home page is at http://mydomain.

However, there's also a second route to the home page - http://mydomain/home/index - which I think

This causes problems because it may be seen as duplicate content, and images are broken on this page.

How can I totally remove this route (so it goes to a 404, I guess?).

I've searched Google but can only find articles on removing Home from routes entirely - not what I need.

I'm using Attribute routing, and this is all that's in the RouteConfig.cs:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// Enable Route Attributes in Controllers
routes.MapMvcAttributeRoutes();

// Fall through all routes
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

The Home Index action has no attribute route on it (as you'd probably expect?). This /home/index route works even on newly generated MVC projects - which I think is a bad idea?

How can I do this?

Are there any problems with removing this route I may not have considered?

thx.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
niico
  • 11,206
  • 23
  • 78
  • 161
  • 1
    Remove the `routes.MapRoute` line then, that's what sets up the route. – DavidG Jul 14 '17 at 14:40
  • Duplicate content is not an issue if you mean it in the context of SEO. This problem is solved by using "canonical urls": https://yoast.com/rel-canonical/ – Jeroen Jul 14 '17 at 14:50
  • I did mean for SEO - I also meant just for my own sanity - I was seeing broken images and took me 10 minutes to figure out why. Also let me flip that round - what is the harm of disabling /home/index? – niico Jul 14 '17 at 16:00

2 Answers2

3

You can block unintended routes that you don't want by using IgnoreRoute().

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.IgnoreRoute("Home");
routes.IgnoreRoute("Home/Index");

// Enable Route Attributes in Controllers
routes.MapMvcAttributeRoutes();

// Fall through all routes
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

However, if these URLs are already in the wild, you should instead setup a 301 redirect to the canonical URL you intended. The simplest way to do that is with the URL rewrite module.

This /home/index route works even on newly generated MVC projects - which I think is a bad idea?

I see this as more of a blessing in disguise. It is an advantage over any SEO competitor using MVC who doesn't do the extra work to remove these routes when you are the one who does.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks - not in the wild and I still need /home/otherstuff - so i guess i only need routes.IgnoreRoute("Home/Index"); – niico Jul 14 '17 at 16:21
0

This is not necessary.

The default route provides optional controller and action names. So if user does not put any name for controller and/or action in path (/Home/Index or /Home in this situation) asp.net will put the right values in application routing.

Whenever you use Url.Action or Url.Route functions it will produce the shortest link for you. So in your website there will be always http://mydomain produced for your root. And for example Category > Index action it will produce http://mydomain/category.

In your website bots will never get to duplicate content if your links are in this way. If you are writing your links manually write as short as you can or simply use Url.Action.

About the images there must be something different, because images are static files. just use "~/imagefolder/imagename.jpg" way to get them. "~" is important to start link from the root of application if you are making your application work on a subfolder in IIS.

Bahtiyar Özdere
  • 1,818
  • 17
  • 21
  • I was seeing broken images and took me 10 minutes to figure out why, it was because I was at /home/index (I'm not sure how I got there). Let me flip this round - what is the harm of disabling /home/index? I don't see the benefit in it existing?! – niico Jul 14 '17 at 16:01
  • Regarding images - they are linked from css within the page - so ~ doesn't work. – niico Jul 14 '17 at 16:04