niico
niico

Reputation: 12739

Remove "Home/Index" secondary route to home page

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.

Upvotes: 0

Views: 1134

Answers (2)

NightOwl888
NightOwl888

Reputation: 56849

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.

Upvotes: 3

Bahtiyar Özdere
Bahtiyar Özdere

Reputation: 1918

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.

Upvotes: 0

Related Questions