I know how to design and implement URL shortening service but I want to know how can I design below
- when I wrote go/google ; it takes me to google.com
- When I wrote go/acc, it takes me to Accenture.com
Right side of / will be configured in URL shortner service but how can I design the left part so that when I wrote go/ in address bar, it hits by service to pick the actual URL mapped with google or acc and redirect
As @StephenOstermiller indicated in his comment, there could be hundreds of ways to implement the desired behavior.
In any way, assuming a simple approach, using the standard Java Servlet API, you could try something like the following.
web.xmlfile, an HttpServlet for processing the URL shortener requests. You can map you servlet to the '/go' servlet path.HttpServletRequest, for example usinggetPathInfoandgetRequestURI. The idea is being able to extract the fragment of the URI that identifies the actual service to which your request should be redirected to. The methods provided by theStringclass could be of help in this step.Mapcould do the trick.302HTTP redirect response with aLocationheader corresponding to the actual service. If necessary, use the information obtained in the step2to append query parameters or any other information you consider appropriate to build the actual service URI.HttpServletResponseprovides the straightforwardsendRedirectmethod for performing this temporary redirection. If you prefer using a permanent redirection, i.e., by using a301HTTP status code, you need to explicitly provide this status code and the correspondingLocationheader; please, see this related SO question.For example:
If, as it seems for your previous questions, you are using other libraries such as Spring and Spring MVC, you can define a simple controller for the same purpose. It could be defined similar to this:
Please, forgive me for the simple code, they are only basic examples of a simple redirection but they exemplify the tasks that need to be performed in order to implement the desired functionality.
I think this kind of redirection mechanism will not be implemented in this way, using an ad hoc application, but probably some kind of L7 network appliance or similar stuff.