PhalconPHP - redirection to home page always adds /index in the URI - redirect

I'm working on my first app in PhalconPHP so I'm deep in the documentation while working, but this doesn't seem to be covered.
Let's say that my app is running on www.myapp.tld. In some situations I need to redirect the user back to the home page and for that I'm using the following code:
if ($haveToRedirect) {
$this->response->redirect();
$this->view->disable();
return;
}
Instead if redirecting to www.myapp.tld, the user is redirected to www.myapp.tld/index. I've tried different redirect calls, but all give the same result:
$this->response->redirect('');
$this->response->redirect('/');
$this->response->redirect('/', TRUE);
In the app's bootstrap I've set the BaseUri to be '/':
$di->set('url', function() {
$url = new Phalcon\Mvc\Url();
$url->setBaseUri('/');
return $url;
});
Is there a way to avoid "index" being added and just have it redirect to "www.myapp.tld"?

If a file is not specified, you will be directed to the index page in that directory by default. You need to specify a file location. Also try URI, not URL

The cause of redirection to "/index" was actually in the Permission class I made several weeks ago. It had:
$this->response->redirect('index');
for every controller that guest could not access to. Since I added new controllers I was continuously redirected to index, and noticed that redirect comes from somewhere else when I removed the conditional redirects I've put in the controller.
Anyway, this is it. Lesson learned - next time grep for 'index' before asking for help. :)

Related

Laravel Redirect to subdomain before loggin

I want to create script that's going to be using different sub-domains as user accounts.
The problem i have is, i would like to create script that's going to be redirecting user just before calling log-in attempt, so the session will be saved on sub-domain, not the main domain.
However, I do not want to share the sessions all around, i want to have it, so one user can be logged on his own sub-domain. So setting up
".example.com"
in config file is not way out for me.
EDIT:
Routes:
http://pastebin.com/kgaKJCWx
controller:
http://pastebin.com/73xsejG4
view:
http://pastebin.com/W8eWGrNA
The simplest solution I can think of would be to just use javascript to adjust the subdomain in the form action attribute. Something along the lines of this:
$('form').on('submit', function(){
var username = $(this).find('input[name=username]').val();
var subdomain = username.toLowerCase(); // you might want to do other things here as well
var newAction = $(this).prop('action').replace('yourdomain.com', subdomain + '.yourdomain.com');
$(this).prop('action', newAction);
});
So when the form is submitted, before it actually gets sent the action is updated with a subdomain. You could also use a placeholder in your original action and replace that with the actual subdomain.

How can I redirect only my homepage to a different URL on a different server?

I am trying to redirect my company's home page (let's call it example.com) to a new webpage I built : example.net, without losing any archive and sub pages, i.e when a user navigates to example.com it will redirect to example.net, but when they click on "about us" I want it to go back to example.com/aboutus.
I can get it to redirect but every time a user clicks on a link (such as about us) the browser will try to load example.net/aboutus rather than example.com/aboutus, which does not exist.
I have tried many different redirect commands, 301 commands, .htaccess as well as adding redirects to the index page. Everytime I get the homepage to open to the new URL
I have attached my index page code.
<?php
/**
* Copyright 2013
* #author
*/
include_once($_SERVER["DOCUMENT_ROOT"]."/example.php");
//print_r($_SERVER);
$pageArr = explode("/", $_GET["q"]);
$_GET["q"] = $pageArr[0];
if($pageArr[1]){
$_GET["z"] = $pageArr[1];
}
if(!$_GET["q"]){
$_GET["q"] = "home";
}
$mDevice = null;
if($_GET["print"] == "true"){
$mDevice = "standard";
}
//Begin defining the main page
$WebPage = new xyWebpage($_GET["q"], $mDevice);
//$WebPage->setCharset("ISO-8859-1");
showPage();
function getPageName(){
return $_GET["q"];
}
?>
I'm not a PHP guy, but depending on your platform you can use either:
MOD_REWRITE Module for Apache or IIS re-write module.
Both of these support re-writing URL's which can match your use case.
I'm curious why you would just not replace your home page that you are redirected from on the same server(s), but in any case, since you already have the redirect working you only need it on the .net server. You will need two rules, one to stop processing if it's a root URL request, and then one for all other requests that will re-write the root back to your .com address. You need that first rule so you don't create a loop between your servers.

Web2Py Redirect after login

I'm trying to redirect the user to another page (default/news) after the login(on default/index) but no matter how much i tried i couldn't get it done. I'm using the login available in the navbar.
I've added this line to db.py
auth.settings.login_next=URL(r=request, c='default', f='news')
Everything in the default controller is the same it includes
def news():
message = "Welcome to News!"
return locals()
I've also tried with this
auth.settings.login_next=URL('news')
Which doesn't work either. Please Help.
Edit:
After searching for days i've found the answer here This is how you do it.
#In layout.html change
<ul id="navbar" class="nav pull-right">{{='auth' in globals() and auth.navbar(mode="dropdown") or ''}}</ul>
# to
<ul id="navbar" class="nav pull-right">{{='auth' in globals() and auth.navbar(mode="dropdown",referrer_actions=None) or ''}}</ul>
And again a change in db.py
#In db.py add these lines:
auth.settings.login_next = URL('news')
That worked for me.
The auth.settings.login_next URL is only a default in case there is no referrer already in the login URL (the referrer is in the "_next" query string variable). The navbar automatically includes a referrer in the auth action links (set to the URL of the current page) -- to override that, you can explicitly specify thereferrer_actions argument and exclude actions for which the referrer should not be set:
{{=auth.navbar(..., referrer_actions=['register', ...])}}
It's now April 2016, and while the original question is still highly relevant, it appears that web2py has changed and the modifications suggested above to layout.html are no longer relevant. There is no reference to "auth.navbar" in the standard layout.html.
The good news is, that all one needs to do is include the python code described above in the default.py controller and it works. Note, I put it in the 'user' function, I'm not sure if that is the best place to put it as most comments just say to put it in 'the controller', without specifying a function.
def user():
auth.settings.login_next = URL('default','dashboard')
return dict(form=auth())
I was able to redirect to a user's home page after login/signup by putting the following code in my product info page.
This way, when they get redirected back to the welcome-product-info page, I check to see if they came from login or signup and direct them to where they should go.
I'd love to find way to use auth.settings.login_next
#
def info():
# If just signed up or just logged in, go to the user's home page and not
# back to this product landing page that referred them
if auth.is_logged_in():
if '/default/user/login?_next=/fnd/home/info' in request.env.http_referer:
redirect( URL( 'user', 'show_campaigns') )
if '/default/user/register?_next=/fnd/home/info' in request.env.http_referer:
redirect( URL( 'user', 'show_campaigns') )
return dict()

Zend AjaxContext, _redirect and hash navigation

first post in SO, even though I've been browsing it for years now to solve those mind-blowing and not so much coding problems.
What I want to do is:
* Use hash navigation (#!/).
* Use Zend controller actions, not php files.
* Load these actions through javascript/jQuery.
So far, I've got this working:
indexController, several Actions, each attached to AjaxContext via addActionContext(), I can call them though my javascript/jQuery file via "hashchange" plugin jQuery(window).hashchange(function(){ bla bla }). I can cycle through actions just fine.
But I want to redirect the user to a login page if he/she is not logged in, which brings me to my issue: How can I achieve that? The redirection is made to another controller (login controller, login action). I was trying something like $this->_redirect('/#!/login/login'); w/o any luck (yes, I've set up an AjaxContext in that controller's init). I keep getting a redirection error ("The page isn't redirecting properly"). If I just type in the address bar "/#!/login/login" I get everything display properly.
Anyway, thanks in advance!
Cheers
Now this starts to get complicated if you ever introduce other non-ajax contexts, but you could add the Ajax context to the Error Controller. Then have the error controller return JSON for the unauthenticated exception if the active context was AJAX (and keep the redirect if the default context was active). Your JS would then listen for that specific error provided by the JSON and manually bounce the user to the appropriate login URL.

How To Redirect From a Control Using MVC2

I have been tasked with creating a user control to live in our master page that allows users to switch between accounts. This way, we can allow users to change their account without having to go back to the accounts page. This seemed like a legitimate and perfectly straightforward task.
I've built the control and added it to the master page using Html.RenderAction. The last step is for me to redirect the user to the home page for that account. In order to do this, I build a route to the home page and attempt return RedirectToRoute(route).
When I attempt this, I get this error:
Child actions are not allowed to perform redirect actions
Anyone have any ideas on how to resolve this or have I coded myself into a box
Thanks in advance
You can cheat with an ugly hack:
[ChildActionOnly]
public ActionResult SomeUserControlAction()
{
// ... some processing
var url = Url.RouteUrl("routeName", new
{
action = "foo",
controller = "bar"
});
Response.Redirect(url);
return null;
}
It's so ugly that I feel ashamed for even mentioning it, but it works.
Another possibility would be to pass the url as part of the view model to the view and perform the redirect in javascript by setting window.location.href to the new url.