Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Building Websites with PHP Contact Form & Sending Email Sending Our Email

William Bruntrager
William Bruntrager
11,473 Points

Problem with redirect on local server (XAMPP)

My redirect statements are not redirecting where I would like them to.

I am using XAMPP for Windows, and I have my project folder set up like so:

C:\xampp\htdocs\emerson

The two $app->get() statements in the code are working fine, so I can get to the main page by entering:

http://localhost/emerson

and the contact page at

http://localhost/emerson/contact

However, the

$app->redirect('/')

statement used after the email is sent in this lesson redirects my browser to:

http://localhost/

How can I get the redirect to go back to the main page for this project, not the htdocs folder?

2 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

Just add your directory to the redirect path:

<?php
$app->redirect('/emerson/');
?>
William Bruntrager
William Bruntrager
11,473 Points

It's a good solution but I was hoping for something more universal. For example, when I upload this project to a server the structure won't be the same, so I wonder if there is a way to write it where I won't have to change the code for local vs. online use.

Jeff Lemay
Jeff Lemay
14,268 Points

You could set up a config.php file and then include it within the files of your project. Then in that config file, set a variable for the root directory. When you deploy to production, you only need to update that config file.

William Bruntrager
William Bruntrager
11,473 Points

Since the links created with Slim-views were working properly I looked for an equivalent function to use in the index.php file. The function I was looking for was called getRootUri(). So I rewrote the redirects as:

$app->redirect($app->request->getRootUri());

$app->redirect($app->request->getRootUri() . '/contact');

That works, and I believe I won't have to change the code at all in order to upload it.

Jeff Lemay
Jeff Lemay
14,268 Points

Looks good!

Just wanted to bring up a point, if this were my project, I'd set the method to a variable so you don't have to write so much each time you create a link. I'm not sure specifically where you would set this in Slim though.

<?php
$root = $app->request->getRootUri();
?>

<?php
/* so instead of */
$app->redirect($app->request->getRootUri() . '/contact');
/* you only need */
$app->redirect($root . '/contact');
?>

Using the following will achieve the same result:

$app->redirect('./contact');
$app->redirect('./');