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

cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

.hacess RewriteRule

Hello, I am trying to clean my site's url, but I'm having some trouble. I use MAMP on Mountain Lion and I've configured MAMP so that my localhost sits at ~Users/myusername/Sites. Here's my code & file structure:

Code

RewriteEngine On
DocumentRoot /~myusername/Sites/mysite/
RewriteBase /~myusername/Sites/mysite/

RewriteRule ^portfolio/$ portfolio/categories.php
RewriteRule ^portfolio$ portfolio/ [R=301]

RewriteRule ^portfolio/images portfolio/images.php
RewriteRule ^portfolio/images portfolio/images/ [R=301]

File structure

Sites/sitename/
    .htaccess
    index.php
    portfolio/
        categories.php
        images.php

The problems

  1. Whenever I add the DocumentRoot line, nothing works, I get the following error:

    Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, you@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log.

  2. Even with the RewriteBase line, the problems below remain

  3. Whenever I type "localhost/mysite/portfolio" (whithout the trailing slash at the end), I get this error:

    *Not Found The requested URL /~myusername/Sites/mysite/portfolio/ was not found on this server. * (btw, WITH the slash at the end, it works, and it redirects fine)

  4. Accessing "localhost/mysite/portfolio/images" never worked, with or without the slash

Help?

6 Answers

This should fix you up. I made a basic mock site based on your .htaccess file and overall site diagram. This works for me

<IfModule mod_rewrite.c>
  RewriteEngine On

  # make our categories page appear as our portfolio page
  RewriteRule ^portfolio/$ portfolio/categories

  # grab the requested url
  RewriteCond %{REQUEST_FILENAME} !-f

  # treat any trailing slash as a php file
  RewriteRule ^([^\.]+)/$ $1.php [NC]

  # route any links without the trailing slash to it's respective php file
  RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

The first rule (RewriteRule ^portfolio/$ portfolio/categories) tells Apache to treat portfolio/categories as portfolio

RewriteCond %{REQUEST_FILENAME} !-f This tells Apache to check the url and see if matches any files in your folder. If it doesn't it'll proceed to the next rule (we don't want images, css files, and js files being sent to our php handling script(s))

RewriteRule ^([^.]+)/$ $1.php [NC] and RewriteRule ^([^.]+)$ $1.php [NC,L] are a catch-all and will try any non-files (images, css, js) with a php page regardless if it has a trailing slash or not. These two rules are what allow us to have pretty urls.

These descriptions may not be exactly how Apache handles these rules but that is my understanding of how they work

The last two rewrite rules allows you to create as many php pages you need for your site without having to define them. So let's say you create a page called contact.php and one called about.php; in your nav you link to them just by doing <a href="./contact" >contact</a> or <a href="./about">about</a>. You won't have to add any rules to handle these pages because our two catch-alls handle them for us.

I hope this helps your understanding.

1-2: I don't think you need the DocumentRoot nor the RewriteBase rules.

3: I wonder if this will start working if you remove the DocumentRoot and RewriteBase rules at the beginning

4: Looks like you're missing the regex end of string marker. The following should fix that issue.

RewriteRule ^portfolio/images$ portfolio/images.php
RewriteRule ^portfolio/images$ portfolio/images/ [R=301]
cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

So... First, thanks for the answer. But, here goes... In my first attempt, I didn't have the DocumentRoot & RewriteBase rules, and it didn't work. I added them thinking that it would fix it, but it continued not working. I left them in my code here, so that people wouldn't give me those as an answer, because I already tried them. (: So, no... Removing them doesn't fix it, unfortunately. ): I tried adding your rules, and accessing the images.php file via mysite/portfolio/images url, but that still doesn't work, with or without the slashes (I get different errors, btw). Still, the only rule that works, is the first one (accessing categories.php file via my site/portfolio url). Very weird. :/

cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

Nice! That worked like a charm! Thank you for taking the time! :D If it's not asking much, would you point me to a link that would explain/help me learn all that stuff you did there?