How can I redirect to a particular view based on url in grails - grails

I am using grails 2.5.5 version, Suppose I am entering url as www.localhost:8080/app-name then it should open the MyHome.gsp, suppose if I give other url ex: demo1.localhost.com:8080/app-name then it should redirect to some login page like login.jsp. How can I do that?

Let me break it up for you :
Suppose I have www.localhost:8080/app-name
suppose if I give other url ex: demo1.localhost.com:8080/app-name
Your app starts here:
Case 1 :/app-name
case 2 :/app-name
The rest of that url is actually DNS and configurating binding tomcat specific or wild card urls to a given application.
So in short you need to filter entire url in the application parse url and redirect in your app accordingly.
You need to then intercept every url with grails 2 there is SecurityFilters which so far as i know works with apache-shiro may also work with spring security.
and within it you need to overall check for something like
URL url = new URL(request.getRequestURL().toString())
String subdomain=url.host
if (subdomain.contains('.')) {
subdomain= subdomain.split('.')[0]
}
that then returns your `demo1` you then redirect it another url if it matches your specific rule.
But as I said you are talking about superficial stuff here as I expressed what the address is or how somone gets to the app has nothing to do with the actual application. This is why IT is big business. Big business not because everyone tries to narrow everything down into one box doing all of this but because when situations likes this happen bigger thinking is needed i.e. do i need a load balancer something like F5 that will split traffic according to a given url and send to another app container that asks for authorisation.
subdomain= subdomain.split('.')[1] in that case then but this leaves room for errors since user could put in demo1.somedomain.com and if that resolves well it is either split by subdomain= subdomain.split('.')[0]
I would do this then
String subdomain=url.host
if (subdomain.contains('.')) {
def splitter= subdomain.split('.')
subdomain= splitter[0]
if (subdomain=='www' && splitter.size()>1) {
subdomain= splitter[1]
}
}

Related

Redirecting a Website request with multiple embedded URL strings

I need to redirect the following example to a new website URL.
https://test.test.com/MDR/LauncherInterface.aspx?host=https://test.test.com/MDR&numid=11111&numid=1111&username=1111&userpass=1111
I need to redirect both URL statements to https://test2.test.com. Using iRules in my F5 load balancer I am able to redirect the first statement but I am unable to figure out how to redirect the second as well.
something like this should help:
when HTTP_REQUEST {
if { [HTTP::host] eq "test.test.com" } {
HTTP::redirect https://test2.test.com[HTTP::uri]
}
}
You can also do this with a local traffic policy, and if the rule is really this simple, that would be the preferred approach as it would be more performant. See this article on DevCentral for details on choosing iRules versus local traffic policy.
Finally, known that using the HTTP::redirect command, you'll be restricted to a 302. Use HTTP::respond to change the redirect code.

Mapping URLs formed by page name to a single different URL

Am having many xhtml pages in my application. First page that a User gets to see is named index.xhtml, when User is asked to update his Profile it will be updateProfile.xhtml etc. When I hit my application my page names gets displayed on the URL. When user is updating profile, URL will be http:/myDomain/myServlet/updateProfile.jsf.
Am interested in knowing whether its possible to map all my xhtml page names which gets displayed on the URL to some other name. For eg., in the above case, I want all the URLs which matches *.jsf pattern to be displayed to user as http:/myDomain/myServlet/myAccount.
I dont see a possible threat if a end user gets to know my page names, but still, I dont have much knowledge on Security/Hacking, so atleast I dont want to display *.jsf in my URL. Because, a user can know that am using JSF.
In JavaServer Faces to rewrite URL you need basically a Filter. In your case your rewrite could be done by something like PrettyFaces http://ocpsoft.org/prettyfaces/.
With PrettyFaces you will be able to configure rewrite patterns and everything you need.
Edit : When creating your own Filter to redirect URL, you also need to create a ViewHandler and override the getActionURL() function so that actions will go to the new URL.

Dynamically creating web page (using portion of URL as a variable)

In a site I'm developing I have a page that presents a post based on the variable in the url:
http://www.mywebsite.com?id=18
So this would load the post who's ID is 18 in the mySQL database.
I would like the create the same effect, but with the url being something like:
http://www.mywebsite.com/articles/title-of-article-18/
Would there be a way to create these pages on the fly with dynamic post content, where the url would originally be created by:
"http://www.mywebsite.com/articles/" + postTitle
You are looking for mod_rewrite and rewriting of urls via htaccess.
What it does is it takes patterns from your url, and the htaccess file detects the pattern redirects that to http://www.mywebsite.com?id=18. Users still see the nice url.
The directory /articles/title-of-article-18/ will not actually exist, and the user never really reaches that location because the htaccess secretly changes the url that the server processes.
See
http://en.wikipedia.org/wiki/Rewrite_engine
or a random tutorial I found:
http://www.blogstorm.co.uk/htaccess-mod_rewrite-ultimate-guide/
try url-rewriting
http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/

How do SO URLs self correct themselves if they are mistyped?

If an extra character (like a period, comma or a bracket or even alphabets) gets accidentally added to URL on the stackoverflow.com domain, a 404 error page is not thrown. Instead, URLs self correct themselves & the user is led to the relevant webpage.
For instance, the extra 4 letters I added to the end of a valid SO URL to demonstrate this would be automatically removed when you access the below URL -
https://stackoverflow.com/questions/194812/list-of-freely-available-programming-booksasdf
I guess this has something to do with ASP.NET MVC Routing. How is this feature implemented?
Well, this is quite simple to explain I guess, even without knowing the code behind it:
The text is just candy for search engines and people reading the URL:
This URL will work as well, with the complete text removed!
The only part really important is the question ID that's also embedded in the "path".
This is because EVERYTHING after http://stackoverflow.com/questions/194812 is ignored. It is just there to make the link, if posted somewhere, if more speaking.
Internally the URL is mapped to a handler, e.g., by a rewrite, that transforms into something like: http://stackoverflow.com/questions.php?id=194812 (just an example, don't know the correct internal URL)
This also makes the URL search engine friendly, besides being more readable to humans.

Can an URL shortener pass parameters?

I use bit.ly to shorten my urls.
My problem - paramters are not passed.
Let me explain I use http://bit.ly/MYiPhoneApps which redirects (let's say) to http://iphone.pp-p.net/default.aspx
Now when I try http://bit.ly/MYiPhoneApps?param=xx this param is not added to the resulting url.
I know I could create an extra "short url" including a paramter - so http://bit.ly/WithParam would result in http://www.mysite.com/somepath/apage.aspx?Par1=yy and so forth.
But what I want is to have a short URL directing to a page - and then I want to add a parameter to this shortened url - which shoul (of course) land at my page.
Is this a shortcome of bit.ly (and others are maybe able to do it) - or does "parameter forwarding" not work with 301 redirections?
Manfred
There's no technical reason why it couldn't be done. The service would simply have to look at what parameters it is being sent, and then rewrite the target URL accordingly.
The problem is that it's not necessarily well defined how to do that.
Suppose you have the url http://example.com/default.aspx?foo=bar, and it has the short url http://foo.com/ABCD. What should happen if you try to access http://foo.com/ABCD?foo=baz? Should it replace the value, so you get foo=baz? Should it append it to make foo=bar&foo=baz? If we include both, which order should they be in?
The system cannot know which parameters are safe to override and which are not, because sometimes, you DO want both of them in the URL, and it may matter what order things are added in.
You could argue "Well, just don't allow this for URLs where parameters are already present", but there's also the issue that it's going to complicate the process a lot more. Without this, you just lookup a key in a database and send a redirect header. Now, you need to also analyze the URL to check for parameters, and append part of the URL you were called by. That requires more system resources per redirect, which may become a big problem if your service is used very frequently - you'll need more server power to handle the same amount of redirects. I don't think that tradeoff is considered to be "worth it".
As mentioned in comments by rinogo and Jurgen
In Clickmeter
Destination URL : www.yoursite.com?myparam1={id1}&myparam2={id2}
Tracking link : www.go.clickmeter.com/38w2?id1=123&id2=abc
After click : www.yoursite.com?myparam1=123&myparam2=abc
In TinyUrl
Destination URL : http://x.com?a=1
Shorten URL : https://tiny url.com/y6gh7ovk
Shorten URL + param : https://tiny url.com/y6gh7ovk?a=2
Resultant URL : http://x.com/?a=1&a=2
Added space to post tinyurl
URL shortening associates a unique key based on a full URL (parameters and all), so it is not possible to pass parameters to a shortening service.
Typically
http://iphone.pp-p.net/default.aspx?param=10
must produce a different key to
http://iphone.pp-p.net/default.aspx?param=22
'Parameter forwarding' is simply not possible in these kinds of redirects, as parameters are not valid parts of a shortened URL is most (if not all) services.

Resources