I want to redirect my '/' uri to a given controller's action. I used to do it via the following code in UrlMappings.groovy:
"/"(action: "highlights", controller: "project")
However, this does not rewrite the url in the browser. It redirects to the correct controller's action, but the browser's navigation Url is still at myProject/. I would like it to be updated to the "correct" uri so that reloads, etc. use the "correct" uri.
I tried:
"/"(uri: "/project/highlights")
but I get a status 404 response.
Any suggestions on how I should proceed?
UrlMappings is only for how to map the url to your controller.action. It won't do any redirect.
If you want to do url redirect, you probably need to setup a http server like apache in front of your application server or the easier way is to just setup a controller.action to do the redirect manually.
"/"(action: "redirect", controller: "project")
In the project controller, and redirect action, just redirect the user to your highlights action.
Your application is deployed to myProject/ context. Hence root ("/") for your application is myProject/. What you want to do is to deploy your application to your server root context. You can do this by setting grails.app.context = “/” in your Config.groovy.
Related
I'm getting this issue:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
This will show www.mysite/controller/login as the site URL
I would like to overwrite www.mysite.com/controller/login with just www.mysite.com, but still go to the login page. Does anyone know how to do it with Cake 2.x?
The behavior It's not exactly as you describe.
What the following does:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
is allow you to type www.mysite.com in your browser, and get the view that www.mysite.com/controller/login renders.
It works like an url rewrite instead of a redirect. Therefore, the above should work as expected. However, if it's not an example, try to name your controller differently, as it may cause trouble with CakePHP.
As stated by Inigo Router::connect() just connects a route/URL to a controller action. So with your defined route you should be able to goto www.mysite.com and your login action will be served (although I'm not sure that it is a good idea to have the base URL act as the login page).
It does not prevent www.mysite.com/controller/login from working as this is one of CakePHP's default routes.
To disable the default routes you need to remove this line from routes.php:-
require CAKE . 'Config' . DS . 'routes.php';
Be warned, if you remove this line you must have defined routes for all your pages in your app's routes file. This is not necessarily a bad thing, Beware the Route to Evil is a good read in regards to this.
As I used the "Auth" component I had to add in the function
beforeFilter()
of my controller this line:
$this->Auth->allow('anAction', 'anotherAction', '**login**');
I have a controller and various action methods. I have url as:
http://localhost:2032/Info/person
I have a requirement that if I enter any wrong controller and action in the URL, it should still redirect it to the above given URL.
Example-if I enter http://localhost:2032/asjjsjdj or http://localhost:2032/info/djwsk it should redirect to http://localhost:2032/Info/person
Please let me know how do I do that?
You need to add on exception handler and redirect from the your predefined URL as mentioned below URL:
How can I properly handle 404 in ASP.NET MVC?
What is the difference between Redirect and RedirectToAction other than their return type?
When do we use each? Explanation with any real life scenario would help me greatly.
I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling id parameter and returning proper view.
RedirectToAction lets you construct a redirect url to a specific action/controller in your application, that is, it'll use the route table to generate the correct URL.
Redirect requires that you provide a full URL to redirect to.
If you have an action Index on controller Home with parameter Id:
You can use RedirectToAction("Index", "Home", new { id = 5 }) which will generate the URL for you based on your route table.
You can use Redirect but must construct the URL yourself, so you pass Redirect("/Home/Index/5") or however your route table works.
You can't redirect to google.com (an external URL) using RedirectToAction, you must use Redirect.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table.
Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
Best Practices: Use RedirectToAction for anything dealing with your application actions/controllers. If you use Redirect and provide the URL, you'll need to modify those URLs explicitly when your route table changes.
In my controller action I return a view and I also need to update the url in the browser
so if the request url is test.site.testsite.com I want to change it to search.site.testsite.com how can I do this.
Thanks
So I'm not positive about sub-domains but HttpContext.RewritePath() allows you to modify the URL. This should give you a start.
You can achieve the above by following,
In IIS host two WebSites (test.site.testsite.com and search.site.testsite.com) both pointing to same physical/virtual directory.
In the Controller action , you can use a filter attribute which will redirect all search request to search.site.com.
I have grails application which has several controllers and actions. Suppose
a user goes to:
http:// www.mysite.com/user/register
which means action register in controller user is executed. what I want is that when the response is received by the browser ( or when the request is made by the browser), the browser URL bar should display (the root path):
http:// www.mysite.com
without executing the default action and controller (mapped to the root path '/' in the URLMappings.groovy).
Is this possible and how to do that?
You can either redirect to root controller+action:
redirect controller: "home", action: "index"
(though this will call the action, is it that you're trying to aviod?)
or call
render template:'some_template', model: '[some: model]'
for root page. It will work if you extract root page elements to a template and have a simple model in it. We, for instance, have no model at all for a root page.
If you wish to always stay on root page, you'll have to use redirect: how can browser go from action page to root page without being told to?
I believe you can use params property of redirect method, or flash variable, to tell the root action what to do and what not to do.