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

Mohammed Khalil Ait Brahim
Mohammed Khalil Ait Brahim
9,539 Points

Redirection not properly working

Hello Everyone, Today I was trying to redirect from a url to another. Unfortunately, it seems my code isn't working :

I want to redirect users from :

http://localhost/contact/?status=thanks

to :

http://localhost/contact/thanks/

Here is my .htaccess file

RewriteEngine On
RewriteRule ^shirts/$ /shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1

RewriteRule ^receipt\.php$ /receipt/ [R=301]
RewriteRule ^contact\.php$ /contact/ [R=301]
RewriteRule ^shirts\.php$ /shirts/ [R=301]
RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ /shirts/%1/? [R=301]

RewriteRule ^contact/thanks/$ contact/?status=thanks
RewriteRule ^(contact/thanks)$ /$1/ [R=301]

RewriteCond %{QUERY_STRING} ^status=(thanks)$
RewriteRule ^contact/\?status=thanks$ /contact/%1/? [R=301]

PS: I cleared my cache several times

1 Answer

Hi Mohammed! This alone worked for me:

RewriteRule ^contact/thanks/$ /contact/?status=thanks

There are some other things you have in there that I don't have in my .htaccess. Here is what I have:

RewriteEngine On
RewriteRule ^shirts/$ /shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1
RewriteRule ^receipt.php$ /receipt/ [R=301]
RewriteRule ^contact.php$ /contact/ [R=301]
RewriteRule ^shirts.php$ /shirts/ [R=301]
RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]
RewriteRule ^contact/thanks/$ /contact/?status=thanks

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ /shirts/%1/? [R=301]

I hope that works for you!

Mohammed Khalil Ait Brahim
Mohammed Khalil Ait Brahim
9,539 Points

I am actually talking about the extra challenge where we have to redirect someone who has an older link to the new link without creating a new page so we have to use RewriteCond ins the same way but for some reason it doesn't work for me

So if user enters this as the url : http://localhost/contact/?status=thanks

The rewrite condition would make it look like this http://localhost/contact/thanks/

In the same way the t-shirt page description url works