Browsing Posts in Apache

Apache mod_rewrite and RewriteRule's are incredibly important as websites get increasingly competitive for placement in the SERP. In most instances, if you're using a framework, there is a strong probability that this will be included in the underlying platform and it'll just be a case of invoking it. This post simply gives you an introduction to the concept and some resources for further reading.

Essentially it boils down to three things:

  1. Write some server-side script to transform your "cruft(ed)" URL's into something more meaningful. i.e. Transform catalog.php?catID=2_4 into something like catalog/kitchen/kitchen-cabinets/ One way you could do this is have an SEO "slug" for each category in your database. In this example cat id: 2 has slug: "kitchen" and cat id: 4 has slug: "kitchen-cabinets". One thing I will stress is it's important to give some serious consideration to your "cruft-free" URL pattern structure. For example: In MVC architecture it's generally considered as controller/action/var1/var2/var3 etc... The reason for designing a solid URL pattern is it will be incredibly useful for (and simplifying) your RewriteRule's!
  2. Now! Hands-up, I'll admit I'm not the greatest when it comes to Regular Expressions and pattern matching. For me it's a combination of (limited) knowledge, Google and trial & error! The next step is to add some RewriteRule's to your Apache config', usually in an .htaccess file, but in rare instances, they can be also be placed in a .conf file.

    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    RewriteCond %{REQUEST_URI} !\.(?:css|png|jpe?g|gif)$ [NC]
    RewriteRule ^catalog/(.*)/$ catalog.php?catID=$1

    This is where it gets a little involved and you'll spend a few hours wading through Apache Docs and Google trying to perfect your .htaccess RewriteRules!

    1. First thing is turn RewriteEngine on. There is an assumption that mod_rewrite is available, for most, it is, but you could wrap your directives in an <IfModule mod_rewrite.c> block to be on the safe side.
    2. The RewriteBase directive, although not always necessary, is used to provide a relative path base for rules, it works somewhat similar to the <base> tag in HTML.
    3. The RewriteCond directive, again not always required, specifies conditions for subsequent rule processing
    4. Lastly, the RewriteRule itself. This is supplied in the format: RewriteRule [pattern] [target] [flags], the [pattern] component of this may take some tuning, as, if like me you're not to great with RegEx. You'll find tons of pointers via. Google, but this page in the Apache Doc's goes over mod_rewrite in a lot more detail, especially their Regular Expressions

      Both RewriteRule and RewriteCond also support the use of "flags" to modify their behavior. For example [NC] = nocase. Some of the other more common flags are [QSA] = qsappend (Query String Append), [L] = last, and [R] = redirect with a valid HTTP status code. For a complete list of flags, go here.

  3. Lastly. A point of note. Without RewriteCond, your rules will be applied to ALL URL's in a document. Including references to CSS/JS/Images etc... which may have a very undesired result if those have been referenced with relative paths in your HTML markup. Two simple fixes. Make all those URL's absolute, or, if like me you don't like this idea, simply add a <base> HTML tag to your pages.


Argghh! This has been a bit of a headache of mine now. Getting VirtualHosts in on Apache to work under Windows with PHP5

I read the Apache documentation over and over, and it appeared quite simple. Add a DNS entry to your local hosts file and update your http.conf file under Apache with the following code :


DocumentRoot c:\apache\htdocs\satinsmooth

So I did, but whenever I tried to browse my virtualhost I got the following error "No Input File Specified" and a 404 error was recorded in the Apache access.log

So then I did the usual. Google'd for a while and came across some information about the way PHP and Apache handle errors so I added some syntax to my http.conf file as suggested

RewriteEngine On
RewriteRule ^.+.php$ /404.html

and tried again. Now I got the official Apache 404 error detailing that it was unable to find the URL BUT it had pre-appended the location of my php.exe file to the URL. hummh!?

So then I Google'd some more, and find this tiny bit of information amongst a host of others. Remark the doc_root entry in your PHP.INI file. Ta Da ! It worked. Now I can happily add virtualhosts using the VirtualHost syntax above. Thank you phpfaqts 

Today is an excersise in trying to get Apache 2.2.4, MySQL5 and PHP5 running in a Windows environment.

Apache 2.2.4
The first problem, unless you have a C++ compiler is locating a copy of the Apache Windows binary. After reading documentation "Using Apache with Microsoft Windows" you can find the win32 binaries here. As this is purely for development work on my laptop I've chosen the non-SSL version ( I may regret this later ! ). After specifying domain, server name and admin email address the install ran efforlessly. One point of note : Skype run's it's client service on Port 80, so you may get a binding conflict. Simply complete the Apache install. Quit Skype, start Apache and then restart your Skype client. Skype will bind to the next available port. You then should be able to browse <localhost> and receive a rather cheeky "It Works!" message.

MySQL 5.0.45
Next MySQL 5. I'm not sure which version of Apache supports MySQL 5. I'm hoping it's => 2.2.2 !  Locating downloads for MySQL is relatively straight forward and a handy windows .msi binary is provided in Community > Downloads

Execution is relatively straight forward, and with a handy configuration tool built into the MSI it's relatively straight forward to configure your database server. Download the GUI Admin Tools ( Administrator, Query Browser and System Tray Monitor ) and your well on your way !

PHP 5.2.4
Very easy to locate the Windows Binaries. installation was also a sinch. Inform the GUI that your configuring Apache 2.2.x and which additional extensions you want installed and the .msi package does the rest.

Next trick is getting everything to talk together.

Mixing it together
Nothing needed. Configuration from within the GUI .msi insured everything was up and running as expected !