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!

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

PHP - Redirecting Old Web Addresses with .htaccess

Hey, I just finished this tutorial. Since I learn by doing, I decided to clean the URLs on my personal Web site.

I'm using MAMP. My first RewriteRule worked just fine. You click on a link and you're directed to a non-index file in the subfolder. (RewriteRule ^portfolio/$ portfolio/portfolio.php) My redirects, however, don't work.

My directory structure is like this: /Applications/MAMP/htdocs/projectfolder.

My RewriteRule looks like this: RewriteRule ^contact.php$ contact/ [R=301].

So if I try to go to localhost:8888/projectfolder/contact.php, I'm supposed to be redirected to localhost:8888/projectfolder/contact/. Instead, I'm getting a 404 error because the redirect is going to localhost:8888/Applications/MAMP/htdocs/projectfolder/contact/. This folder DOES exist in my directory structure, but I don't know why it's trying to go there instead of to localhost:8888/projectfolder/contact/.

I've added and removed the leading slash and that doesn't work. In fact, it doesn't seem to matter what changes I make to the .htaccess file, I see the same 404 error trying to find the same directory that doesn't exist. I've cleared my cache and nothing.

On a related note, it seems I don't need the leading slash as shown in the tutorial. The rule works fine without it. Not sure why and I haven't taken the time to try and wrap my head around it. My file structure is similar to that in the tutorial.


NOTE: I went ahead and uploaded my files. The .htaccess file works on my remote server...and I have to add the leading slash. I would still like to understand why this doesn't work locally.

10 Answers

Kazimierz Matan
Kazimierz Matan
13,257 Points

Redirects and .htaccess in general are very dependent on working environment. In my XAMPP I've made the same directory structure and exactly the same .htaccess file. And when I try to display localhost/girlswritecode/contact.php I get yet another different result - Apache redirects me to http://localhost/contact/, which is nor desired location, not the one you were redirected to.

So, it looks like the effects of redirect were determined not only by rules in this file itself, but also by some other factors, like:

  • your/mine .htaccess in upper directory of local development server,
  • your/mine .htaccess in upper directory of remote production server,
  • your/mine Apache configuration files of local development server,
  • your/mine Apache configuration files of remote production server (which often we cannot change)...

...which change the way redirect works.

At this moment I give up:(

If you absolutely want to resolve this problem you can try to ask the question at Stack Overflow. This is where I found the solution for many of my problems.

Regards Kazik

Kazimierz Matan
Kazimierz Matan
13,257 Points

Some new versions of MAMP by default ignore .htaccess file. Please look at your http.conf file (somewhere in MAMP directory, or maybe there is a link in control panel) and search for "AllowOverride None". Something like this:

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride None

...and change "None" to "All". Then restart your MAMP server. Maybe it will help.

Kazimierz Matan
Kazimierz Matan
13,257 Points

When people use RewriteRule they typically want to do a clean address, just like in your first, working case (RewriteRule ^portfolio/$ portfolio/portfolio.php). Here you tell server that portfolio/ is a kind of alias and that real file is a portfolio/portfolio.php one. But what is the purpose of doing your second redirect ( RewriteRule ^contact.php$ contact/ [R=301]). This indicates a directory - does an index.html file exists in this directory? Another posssibility - try to include your directory "projectfolder/" in your redirects (I assume contact is a subdir of this dir).

Thanks, I checked and mine is already set to "AllowOverride All." Hmmm...

I had a pre-existing site. After going through the one of the tutorials on clean URLs, I decided to clean up mine. I needed the redirects in case people bookmarked the old site. Yes, contact is a subdirectory. I tried your suggestion and it didn't work. Since it works on my live server, there must be something in my MAMP configuration. I could be happy, I guess, knowing it works on the live site, and try to figure out the development site later. Thanks anyway.

Kazimierz Matan
Kazimierz Matan
13,257 Points

It's hard to say more not knowing the full content of your .htaccess file (there are various nuances, such as the order of the rules) and full directory/files structure.

Okay. Well, I'll give this one last shot. I appreciate your help.

Here's my directory structure: Applications/MAMP/htdocs/girlswritecode/about/index.php Applications/MAMP/htdocs/girlswritecode/contact/index.php (I have several other folders set up like these two)

Here's my .htaccess file contents: RewriteEngine On

RewriteRule ^portfolio/$ portfolio/portfolio.php

RewriteRule ^about.php$ /about/ [R=301] RewriteRule ^contact.php$ /contact/ [R=301] RewriteRule ^hire.php$ /hire/ [R=301] RewriteRule ^portfolio.php$ /portfolio/ [R=301]

So, this works on my production server, but not my development server, which is MAMP.


Kazimierz Matan
Kazimierz Matan
13,257 Points

OK, I'll try to reproduce these conditions in my local environment (although it's XAMPP, not MAMP) and will try to find the cause. I will try to answer within the next day.

You're awesome! Thanks!

Kazimierz Matan
Kazimierz Matan
13,257 Points

Is your .htaccess file located in /htdocs/ or in /htdocs/girlswritecode?

Oh, yeah, I guess that is an important bit of info. It's in /htdocs/girlswritecode simply because I have several Web projects. I guess once I finish a project, I should move it to an archive instead of leaving it in my htdocs folder because it's not reflective of my production server setup.

Thanks for trying!