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

gareth connop
gareth connop
14,865 Points

.htaccess thanks rewrite

Under the extra credit section of Enhancing a simple php application/cleaning urls with rewrite rules, I'm having trouble getting the rewrite to thanks/ to work and getting a 500 error.

In my .htaccess file I have:

RewriteEngine on
RewriteRule ^?status=thanks#error$ /thanks/ [R=301]

5 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

The rewrite rule has these parts:

RewriteRule <one> <two> [flags]

Part one of the rewrite rule matches what appears in the web address, which is meant to be this:

/contact/thanks/

(You'll need to get the right characters to match the beginning and the ending, as well as the optional trailing slash.)

Part two of the rewrite rule matches what file you want to load when that URL is visited:

/contact/index.php?status=thanks

A few things to note:

  • I think you have the two parts reversed.
  • The #contact hash is not something the server even knows about. The browser will handle that, and you can leave it out of the rewrite rule.
  • The [R] flag will redirect, changing what appears in the address bar. That's not what we want to do here. We want the web address to say [/contact/thanks/] still, but we want the server to load up the code at [/contact/index.php].

Does that help?

Ian Carroll
Ian Carroll
1,901 Points

Could you paste any logs that you might be able to get? I'll check your syntax in a sec.

gareth connop
gareth connop
14,865 Points

Hi Randy,

My form is half way down my home page hence the lack of contact folder and the additional of #error (used on submit to reload at the form error).

From your advice above I have been able to get the rewrite to work and show localhost/thanks/#error but still need to find a way to hide #error.

At the moment my .htaccess code is:

RewriteEngine On
RewriteRule ^thanks/$ /?status=thanks

Do you or anyone else know how to do this?

Here is an additional section of code from my home page, which references #error:

<div id="contact_header">
    <h2>Get in touch</h2>
    <?php
        if (!isset($error_message)) {
            echo '<p>Just fill out the form below.</p>';
        } else {    
            echo '<p id="error">' . $error_message . '</p>';
            }
        ?>
</div>

<form id="form" method="post" action="<?php echo BASE_URL; ?>./#error">

Thanks

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

You can't exactly hide #error. Rewrite rules run on the server, and the #hash never even gets sent to the server. The browser uses the #hash to navigate down the page.

Instead, you could redirect to the page without the hash. With JavaScript, you could look on the page to see if a paragraph with an ID of error exists and (if so) use JavaScript to scroll down the page to that location.

Does that help?

gareth connop
gareth connop
14,865 Points

Thanks again randy. I get the concept not too sure on the execution though.

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

I'm glad that helped. Do you have an idea of how to get started trying the JavaScript implementation? Give something a try, and feel free to post your code here for us to take a look at!

gareth connop
gareth connop
14,865 Points

I managed to do what you suggested and used JavaScript to scroll to the element if it exists on the page, which works really well, but then thought that due to some browsers having JavaScript turned off, that I would have to leave it as it was.

In the end I just changed the element ID to message, which looks better than having #error in the URL.

So now I have:

localhost/thanks/#message for the thankyou page & localhost/#message for the error page

Thanks again Randy and if you think of any other solutions to this, let me know.

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Excellent!

There actually is a better way that I plan to cover in the next PHP project: Ajax. Instead of requiring a page refresh for the submission, we could use JavaScript to submit the form behind the scenes. This is more advanced, of course, and takes a bit of work to set up a PHP service that we could write Ajax code to use.

It should be a good set of videos: stay tuned!

gareth connop
gareth connop
14,865 Points

Sounds good. When are these videos due to release?

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

They are currently just ideas in my head, so they'll still be a bit off. We'll add them to the roadmap once we have something a little more concrete!