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 Enhancing a Simple PHP Application Cleaning URLs with Rewrite Rules Redirecting Old Web Addresses

Redirecting Old Web Addresses Help

My redirects don't seem to be working i have placed all the files that are suppose to be redirected to in their folders but the issue is that when you type in the filename that apache should search for and redirect to i keep getting a 404 error all my project files are in a folder called shirts4mikeV2 and even when i add this to the start of the redirect i am still getting a 404 error it can't be found rather than it redirecting to the file that i am specifying

Here is the code in my htaccess

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

RewriteRule ^shirts4mikeV2/receipt.php$ /receipt/ [R=301]
RewriteRule ^shirts4mikeV2/contact.php$ /contact/ [R=301]
RewriteRule ^shirts4mikeV2/shirts.php$ /shirts/ [R=301]
RewriteRule ^shirts4mikeV2/about.php$ /about/ [R=301]

I keep getting a 404 error can someone help please

2 Answers

in your apache's httpd.conf file make sure in the <Directory> section for the DocumentRoot that you have AllowOverride All by default it is set to None. This will keep your htaccess file from working.

I have done this and still i am getting the same error :(

give me a minute I am installing MAMP now.

Thanks dude its just that in his videos he places all his files in htdocs but i have different versions of the project in htdocs ie V1 V2 V3 so at the root of my server i have different folders which have different files could this br the problem

Can I make a suggestion. Instead of trying to do all these rewrites try doing something a little simpler.

drwxrwxr-x   7 jandrews  admin  238 Jul 22 20:25 .
drwxrwxr-x@ 23 jandrews  admin  782 Jul 22 20:07 ..
-rw-r--r--@  1 jandrews  admin  337 Jul 22 20:21 .htaccess
drwxr-xr-x   2 jandrews  admin   68 Jul 22 20:24 shirts_v1
drwxr-xr-x   2 jandrews  admin   68 Jul 22 20:24 shirts_v2
drwxr-xr-x   2 jandrews  admin   68 Jul 22 20:25 shirts_v3
drwxr-xr-x   3 jandrews  admin  102 Jul 22 20:21 shirts_v4

This is my MAMP htdocs directory. I have 4 shirts versions. the tutorials however use just shirts. What I would do is instead of trying to do all this rewrite business just use a symbolic link to link a shirts version to shirts.

ln -s shirts_v4 shirts

This creates a file called shirts which is a symbolic pointer to shirts_v4 which the normal rewrite for the tutorials can follow.

drwxrwxr-x   8 jandrews  admin  272 Jul 22 20:27 .
drwxrwxr-x@ 23 jandrews  admin  782 Jul 22 20:07 ..
-rw-r--r--@  1 jandrews  admin  337 Jul 22 20:21 .htaccess
lrwxr-xr-x   1 jandrews  admin    9 Jul 22 20:27 shirts -> shirts_v4
drwxr-xr-x   2 jandrews  admin   68 Jul 22 20:24 shirts_v1
drwxr-xr-x   2 jandrews  admin   68 Jul 22 20:24 shirts_v2
drwxr-xr-x   2 jandrews  admin   68 Jul 22 20:25 shirts_v3
drwxr-xr-x   3 jandrews  admin  102 Jul 22 20:21 shirts_v4

Notice the shirts -> shirts_v4 file which signifies shirts points to shirts_v4. When you want to go to shirts_v5 simply delete shirts and recreate the link to shirts_v5

This is something that gets done a lot in real life applications so you may as well learn it now.

Looks a bit advanced but i definetly agree are there any in depth tutorials you can suggest it does seems like an easier way to redirect a user

There's really no tutorials for symbolic links basically you just make the symlink and then in your htaccess file all your rewrites point to shirts instead of shirts_v1 etc...