0

I have a lot of URLs I need to redirect to the homepage (the reasons are convoluted - and SEO isn't important so be assured this is what I need to do).

eg

mydomain.com/hello
mydomain.com/bye
mydomain.com/whatever

& 30 more all need to be permanently redirected to the homepage.

I could setup all these routes individually in RouteConfig.cs like this:

routes.MapRoute("AboutUs", "aboutus", new { controller = "Home", action = "Index" });

However that would be a lot of work, and this is a frequent requirement.

I can find nothing on Google.

How can I use a list or some more efficient approach for this?

EDIT:

Dynamic routes seems overkill - I just need a list of URL's I can redirect somewhere without crufting up my RouteConfig.cs. The idea is a terse simple quick solution - rather than implementing something complex like creating dynamic routes.

niico
  • 11,206
  • 23
  • 78
  • 161
  • You want those routes to be permanently redirected to homepage or just invoke Index action of HomeController each time they are requested? – tmg Jun 12 '16 at 19:17
  • Possible duplicate of [Is it possible to create routes dynamically in .NET 4?](http://stackoverflow.com/questions/6834112/is-it-possible-to-create-routes-dynamically-in-net-4) – Orel Eraki Jun 12 '16 at 19:35
  • 1
    You could do a catch-all route to match anything (placed last). But I think [IIS Url Rewrite](http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module) with (thirty) permanent redirects would be better and catch only these specific routes. – Jasen Jun 12 '16 at 19:48
  • As Jasen said, IIS URL Rewrite module is the best option. However, if your requirement is to add URLs so frequently that you need to dynamically add 301 redirects to the application through its UI, you could combine a [data-driven custom `RouteBase`](http://stackoverflow.com/a/31958586/181087) with [a controller and view to properly handle 301 redirects](http://stackoverflow.com/a/36168395/181087). – NightOwl888 Jun 13 '16 at 10:46
  • I want them permanently redirected to the homepage. I do not want *any* non-existent page redirected to the home page - only this specific list so a catch all isn't an option. I would rather not be reliant on the IIS Rewrite module if possible. Editing a .cs file to add routes is sufficient (not all that frequent updates required). thx. – niico Jun 13 '16 at 11:05
  • 1
    Routing itself only directs a URL to go to a specific controller action. Routing alone is not capable of doing an HTTP 301 redirect (redirect permanent) - for that you need a controller. If you just want to edit a config file to add 301 redirects, IIS Rewrite Module is what you are looking for - you just install the module, add elements to `Web.config` and your are done. See examples [here](http://www.geekytidbits.com/redirect-iis-7-url-rewrite-module/) and [here](http://www.sherweb.com/blog/how-to-create-multiple-301-redirects-url-rewrite-map/). – NightOwl888 Jun 13 '16 at 14:06
  • @niico If you do not want to use IIS Rewrite Module, you can use a catch-all route with an IRouteConstraint to match only your legacy routes and then an IRouteHandler to perform perma redirect. – tmg Jun 13 '16 at 22:15

1 Answers1

0

I think an option could be to use javascript, when you generate that url and save it to a hidden field, and then just set all your ancors href to the value of the hidden field

Ermir Beqiraj
  • 867
  • 11
  • 24