iis - How to redirect any url that have hyphens to home url in asp.net mvc 5 -
i creating new site 1 of client. old site developed using wordpress , hence has 100s of broken urls below:
http://www.example.com/best-gym-in-town/ http://www.example.com/video-gallery/ http://www.example.com/fun-stuff/ http://www.example.com/are-you-a-diabetes-patient/ http://www.example.com/john-in-media/ http://www.example.com/photo-gallery/ http://www.example.com/nutrition-program-that-suits-your-lifestyl/ http://www.example.com/our-range-of-fitness-tests/ http://www.example.com/corporate-group-workshops/some-article/another-article
i developing new site in asp.net mvc 5. want write httpredirect rule in web.config can redirect of above url home or specific page.
so far how thinking of solution
<location path="about-me"> <system.webserver> <httpredirect enabled="true" destination="/home" httpresponsestatus="permanent" /> </system.webserver> </location>
but have write 100s of such entries in web.config. looking better , efficient alternative
in global.asax, hook application_beginrequest
method , redirect homepage there
protected void application_beginrequest(object sender, eventargs e) { try { if (httpcontext.current.request.url.absolutepath.contains("-")){ httpcontext.current.response.redirectpermanent("/"); } } catch (threadabortexception){} }
optionally, hook application_error method inside global.asax instead , detect 404's there
protected void application_error(object sender, eventargs e) { var exception = server.getlasterror(); if (exception != null && exception == 404) { try { response.clear(); server.clearerror(); response.redirect("/"); } } catch (threadabortexception){} } }
Comments
Post a Comment