Quicksearch |
Tuesday, July 25. 2006
Posted by Ben D. Benner
Comments (0) Trackbacks (0) Mod_Rewrite it changed the way I look at web programming
Where to start.
I was introduced to Mod_Rewrite by a pair of gentlemen who run a badass web development firm out of the Dallas Metroplex area, DeverGoodwin . I sort of understood what was used for. But quite honestly, my initial understanding was about the same as what someone (non-developer/geek) would get from looking at the following picture from the Apache documentation website. ![]() Yeah, that is what I am talking about. Looks powerful right? But what does it do exactly? It is hard to explain in just one word or even one sentance. At least without first describing what I call a "Page Server". Unfortunately I might be one of a few that use that name to describe it, if anyone out there knows a more common name for the following description, please post a comment about it. But a "Page Server" is the idea that you write your web page application in a manner that you basically maintain a small number pages that people can access from the web. But these pages (or in some cases, you only have the index.php/asp/jsp) dynamically build their content based upon a "page code" or some other type of parameter. This might leave you "non-propeller head" folks wondering what I am talking about. Have you ever visted a website where the URL ends up looking like the following? http://www.somedomain.com/index.php?pg=profile This is what I typically a "Page Server". So the concept is that rather than have to write a page that is... http://www.somedomain.com/profile.php ... you instead .... http://www.somedomain.com/index.php?pg=profile Of course the obvious question is. How exactly does that make things easier? Well when one has to code web pages that look pretty and have a consistent feel you have to deal with making each page look exactly a like. Say for instance in the header. Each page should have the same graphics across the top (or only slightly different) and they should have the same links (again or slightly different). When you are programming straight HTML, this means you have to copy and paste the code into each page. When your coding in a scripting language like PHP or ASP you get the luxury of doing an include() type of function and just doing... include(header.php); In either the Copy&Paste or include(header.php) arena you will encounter a critical mass when it comes to the number of pages you are maintaining. This can be extremely evident when one needs to update a Copyright in the footer or change up the name of one of the links. Well if you only have a few pages like 2 or 3, no big deal. Copy&Paste your heart out. But when you start having 10,20,30 pages in HTML you REALLY, REALLY should using a scripting language. Of course once your using a scripting language the benefits to using a "Page Server" design are not entirely seen in the begining, it could even be more of a philosophical debate on why to build a Page Server rather than just make individual pages using the include(header.php) methodology. I prefer the Page Server method. As it allows you to centralize your functionality, rather than having to update all sorts of pages with new path names or what not you can just update one file. If you work your switch() statement appropriately you can build new functionality into your web app quick and easily. Of course a Page Server methodology means that you are bringing folks back to the index.php through links within your app. This means that if you are pushing folks to the central index.php. But someone decides to look for... http://www.somedomain.com/about.php They will likely get a 404 error. You could build a custom 404 to try to get around some of that. This is one of the first things that I saw about mod_rewrite, it allows you control someone's visit of your site completely. If they search for a page you don't actually use it to simply force them back to your index page. Of course I am jumping a head a little bit. As we now have a basic understanding of what I am calling a Page Server, let's start looking at how Mod_Rewrite can aid the functionality of a Page Server designed app. Mod_Rewrite allows you to take the URL that is requested and break it up into variables that can be passed to the index.php page. This means that to achieve the same thing you get with... http://www.somedomain.com/index.php?pg=profile&user=fajita using a URL like this... http://www.somedomain.com/profile/fajita/ Yes, that is right. The "/profile/fajita/" is translated behind the scenes to the index.php as pg=profile&user=fajita. This means that Mod_Rewrite allows for the creation of clean URLs rather than index.php?var=blah&var2=blah2. This means that your application will look better to users as well as search engines. Now another thing that I love about mod_rewrite is that when I have done apps before using the include(module.php) I was always concerned about someone finding and browsing my module pages directly. Which meant I had to do security checks within the module to determine whether or not the person was logged in and then give a response like "unauthorized access" if they weren't and so on. This meant that I was spending time writing or Copy&Pasting that logic into a module page that was really about the module but rather trying to prevent someone from accessing the page directly. With Mod_Rewrite you have the capability to simply force everything back to the index.php page. You don't have to worry about the security checks. This overall makes it easier to code a modular web application without fear of someone browsing your module pages directly. Here is an example of a few mod_rewrite rules... Rule that allows for a one directory variable: RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?pg=$1 [NE,QSA] The "^" opens the part of the rule that looks for what is in the URL. In this case I am looking for letters (upper or lowercase) and numbers. Whatever is found is submitted to the index.php page. The QSA appends the query string useful more so when dealing with a URL like... http://www.subdomain.com/profile/fajita.php?view=pic1 However, I usually leave it on there. Yes, the fajita.php, you can write a Mod_Rewrite rule that allows you to use non-existance file names, rather than just non-existant directories as variables as well. Rule for 2 directories: RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?pg=$1&search=$2 [NE,QSA] Rule for 3 directories: RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?pg=$1&search=$2&type=$3 [NE,QSA] Rule for one directory and a file name: RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)\.php index.php?pg=$1&id=$2 [NE,QSA] Rule for just a file name: RewriteRule ^([A-Za-z0-9-]+).php$ index.php?pg=$1 [QSA] To me Mod_Rewrite changes the way I think about web programming, I think of it more as programming an cohesive application that has different modules that are called using a centralized Page Server. Rather than just building individual HTML pages or scripted pages using the include() methodology. It seems more like real programming. I have read over the Apache tutorials and there are a TON of different things you can do with Mod_Rewrite. I will post some more on it later. While seeing what DeverGoodwin could accomplish using Mod_Rewrite set me down this path. I would not have been able to finish my first project without the assistance from an interestingly named website. www.ilovejackdaniels.com, I found the mod_rewrite cheat sheet they provide to be very helpful. Laterz Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
Recent EntriesNew Blog -- Ready Enough
Wednesday, June 18 2008 New Blog - Attempt #2 Tuesday, June 17 2008 Ummm... It's Hot! Sunday, May 25 2008 30,000 Feet, 18 Hours and a bag of cheeseburgers Monday, May 19 2008 It's A Grind Thursday, May 15 2008 ArchivesSyndicate This BlogStatisticsLast entry: 2008-06-18 01:19
431 entries written
136 comments have been made
CommentsCall Center Software about 30,000 Feet, 18 Hours and a bag of cheeseburgers Sun, 15.03.2009 10:23 She described them as Sales Guys and that they were going to Manila for three weeks. Apparently they are rotating sales [...] furnace filters about My mom is a blogger! Mon, 22.09.2008 04:13 Wow your mom is "My mom is a blogger!" i hope my mom too... CJ about The deed has been done. Sat, 23.08.2008 04:54 I have hair very similiar to yours. I thinks its because I told a hair stylist to make my hair more curly. my hair went [...] |
