How to use mod_rewrite with .htaccess (Part 2)

This is a continuation of my first article you can read here on how to use mod_rewrite with .htaccess. It can be a pretty daunting task getting all of this to work together seemlessly but once you get it working, it’s so easy and so sweet to use. In the last article

  • How to enable .htaccess files
  • How to include the mod_rewrite module in the httpd.conf (Apache Config)
  • How to turn on .htaccess permissions in Apache config
  • How to turn on mod_rewrite in a .htaccess file
  • How to write mod_rewrite rules in .htaccess files
  • How to create dynamic variables with preset tags

In this article we are going to handle a little more regex but also a little more of Apache’s built-in functionality.

Lets say you have a website, http://www.crainbandy.com and you want to accept anonymous commands to the “root” URL, or http://www.crainbandy.com/command instead of using a placeholder like we did in the last article. We used the placeholder cmd/commands.

What are placeholders?
When you begin to work with .htaccess files and mod_rewrite for your URI’s you’ll realize that you have to have a placeholder to extend your URLs off of, example
http://www.crainbandy.com/cmd/signup or http://www.crainbandy.com/cmd/login the placeholder here is cmd because it is familiar to you. You are already looking for this variable so there aren’t any surprises, everything after the placeholder is turned into the command and you can deal with it accordingly. After you have a working placeholder you extend with things like /functions or /send etc. Let your imagination take it’s places. I hope you understand placeholders for URLs they are really essential with mod_rewrite (until you get into heavy regex & apache voodoo which I don’t intend to get into in this article)

What if I just want to have /login instead of /cmd/login?

Good luck, not going to happen.
…just kidding.

Well that’s the problem I ran into. The reason this was particularly hard to find an answer for is because Apache automatically searches for a folder first, then a file. So if you go to crainbandy.com/login and it’s not a folder, nor a file, nor does apache have a rule to deal with this it just crashes giving you a 404 error… Hardly the desired result. So I, being the crafty being that I am thought about using a folder /login to hold the login script, but I just don’t want to have folders for all of my root URL functions (root URL meaning, right after the base url or www.crainbandy.com/THIS IS THE BASE AREA/THIS IS POST BASE/ THIS IS POST POST BASE… etc) so I thought ah well, what if I redirect the 404 to my index.php and parse it there? Cool. You just run into more problems so then I decided to stop being a genius and use Google & the Apache manual to find this

Enter the world of RewriteCond’s and RewriteRules working together in a harmoniously violent and tasty artistic jellyish fashion.
In english, you can tell apache “If there isn’t a folder, or a file, do this” which is exactly what we need, so here’s the code.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ index.php?cmd=$1 [L]

Oh so nice. notice how the start of the rule … “^” doesn’t have a placeholder? like ^cmd or ^action it just simply has that scary regex statement that like I said in my previous post, says “everything”, isn’t that crafty? All of that !-d voodoo is built-in to Apache. Basically says, Find a filename or a folder … (-d) if you can’t find it, do the rewrite rule below. So! Hope that helps you guys some more! This would allow for what I call, base URI’s (actions & commands and all kinds of stuff without placeholders) which is sexy.

You could use this for a myspace type result with the whole, “myspace.com/users_url” thing. So, hope this helps you make your site a little bit cooler.

Related reading

6 Responses to “How to use mod_rewrite with .htaccess (Part 2)”

  1. Ray  on October 17th, 2006

    Holy hell mate….

    I have been searching for this solution for about 3 days!! Thanks a million, I owe you…

  2. Stephen  on November 16th, 2006

    WOW! Thanks for the help. I’ve banged my head against the wall for a couple hours and came up empty. Your code did the trick.

    Thanks much!!!!

  3. hanren  on November 25th, 2006

    plenty of people at the joomla community had these
    “search engine friendlyW problems

    your article had just solved mine

    thanks a lot .. been having these problems
    for over the week

    thankyou again you really saved my day

  4. Michael  on July 10th, 2007

    Sweet thanks a million. I was also searching for a way to do the mysite/myprofile type urls for the longest time.

  5. MIke  on November 3rd, 2007

    Hey I was wondering if you could help me out with something. I have been coding a site the last couple of months and have been doing this

    http://page/2007/02/28/page_title

    I use php to break the url into an array and use those in php to access my mysql database etc.

    I currently use the following as an htaccess entry this particular page

    #this page
    RewriteRule ^page/(.*) page.php?user=$1

    Now if there are other php pages that I need to use the the url in an array I have to write the same thing

    #new_blog_entry
    RewriteRule ^otherpage/(.*) otherpage.php?user=$1

    Is there a way to just have one entry for all pages in my site? As it its I have 20 entries in my .htaccess and it dosen’t look like i’m doing it right.

    Oh one more thing. I had gotten your

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/.]+)$ index.php?cmd=$1 [L]

    entry to work on one occasion but since I made some changes to my site It hasn’t worked anymore. I include a page in my index file that queries my mysql database for the url right after the mysite.com/thisurl and if it is found it redirect to the users profile. Any ideas how I might fix this? Thanks alot.


Leave a Reply