Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to manage 301,404 and other redirects. I can create a table in the sql server database (which will contain the old and new url). How to call them in the front end server when anyone hits the old URL.

Solution for non-MVC preferred. Should be fast.

I appreciate any help. Thanks.

What I have tried:

I tried using it manually but it's not feasible at the moment with so many URLs.
Posted
Updated 22-Oct-18 6:53am

There are a multitude of ways that redirects can be handled. The order that these are in is signifigant in the fact of how much application overhead is created, if any; the less overhead that is onvolved the more performance your website will have.

1. At the (web) server level. This would be normally done via an .htaccess file or in ASP.NET sites could be via the web.config file. The advantage with this method is that there is little if any impact on the application so you will see near 0 performance impact. The disadvantage to this method is that it is done via the configuration file for the application and generally requires an application restart.

2. Within the http pipeline. This would be via a module (aka hook, handler). This can be written to intercept every request, and determine if we should let it go to the website or if we need to do something else; such as a redirect, return a 404, etc. The advantage is that this is still early in the pipeline and has little performance impact, and is very powerful. The disadvantage to this is that it is the hardest method to effectively implement. This is a very powerful option and can crash your site with a simple error.

3. Server side file. Every file has a programmatic redirect located within it. Very tedious to maintain. You could also use this within an MVC controller. The entire application is needed to run this.

4. Client side file. Every html file has a <meta refresh> element in it. Same disadvantagse as the server side variant, and it requires the browser to actually process- along with the fact that <meta refresh> may not be supported or disabled.

How to with a DB. You could implement all 4 methods utilizing a DB. The method you choose to implement your redirect solution will dictate how to integrate the DB. If you are using a configuration/htaccess file you would need to rewrite the block of redirects based on your DB table.

I personally use method 2, as I needed to develop an iHttpHandler for security purposes in an MVC based CMS. I load up all of the redirects into a collection and scan that for every request. Earlier versions accessed the DB on every call but this created overhead which I needed to get rid of.
 
Share this answer
 
Using a database to manage this is - well - stupid. Google will provide a wealth of alternatives, such as this one.

How to automatically redirect a browser to another web page from one of your own[^]
 
Share this answer
 
Comments
Mr_cool 22-Oct-18 8:02am    
This won't work for me. Thanks anyways. I have pages that don't exist and need to redirect them to an existing page.

example: http://www.example.com/hello111 (old url and doesn't exist) should redirect to http://www.example.com/hello-world (exists)
You could specify a custom 301 and 404 error page in the web.config, and in the code behind for that page (in the PageLoad method?), evaluate the 404 response, which should include the faulty url, and write code to redirect to the appropriate page.

So, if the bad url was http://www.example.com/hello111, you could set a string to the correct url (http://www.example.com/hello-world).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900