Why would I use an htaccess file?
Developing our client’s online presence is a big part of what we do here at CGI Interactive. Besides keeping me gainfully employed, a professional and easy to use website keeps you looking good to your current and future clients. Arguably, nothing is more important than fresh content. Last I checked it was 2009. No more animated GIFs with a letter going into a mailbox or frame based navigation with awkward double scroll bars. No, these days everything is slick and content managed, dynamic and free; for computers and technology, it’s like the 60’s. But with Woodstock days away your homegrown content management page URLs are looking totally square, man. Who wants to look at something like this:
http://www.yourwebsite.com?page=home&visitor=400&level=5
Personally, I don’t care. I’m a developer and I know the world can be cruel to a newbie trying to get their feet wet in the “variable passing through the url” pool. But any marketing person can tell you with all the zeal of a red carpet reporter that your style is just not going to cut it in Hollywood. People want to look at this:
http://www.yourwebsite.com/home.html
or
http://www.yourwebsite.com/home/
Maybe your marketing team has a great idea for a microsite, but you’re running short on budget to get hosting for it. Sure seems like a waste to pay $10 a month for a whole server just to host a few small pages. With htaccess you can host the files on an existing webserver and map requests for a specific domain to that folder only.
Maybe you’ve got a blog and you’re tired of providing free advertisments for miracle diets but don’t want to shut down comments completely. If you’ve got the IP address of the offenders you can block em’ out for good.
Htaccess is a truely versatile tool but getting into it can be intimidating. First things first:
Configure Your Server
The easiest way to do this is to just call your hosting company and ask if htaccess is enabled. For those unfortunate developers, like myself, who are responsible for their own server the process is a little more challenging.
-For an Apache Server-
1. Open http.conf in your favorite text editor.
2. Find this line:
;LoadModule rewrite_module modules/mod_rewrite.so
3. Remove the comma so it looks like this:
LoadModule rewrite_module modules/mod_rewrite.so
4. Find this:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>
5. Change it to this:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>
6. RESTART APACHE!!!
-For an IIS Server-
1. Cry deeply because you do not have an apache server.
2. Apply for stimulus money and purchase this product: http://www.micronovae.com/ModRewrite/Purchase.html
3. Alternatively, convert to Apache.
Okay, so now you’ve got apache restarted and it should be ready to do your bidding. Test this by creating a file called .htaccess in the root directory and typing some nonsense into it. This should throw an ugly configuration error when you try to access the server. This means apache is looking for and responding to the .htaccess file. It bears mentioning that errors in your .htaccess file will cause your server to be inaccessable, so if you’ve got live sites and slow fingers you might want to play around on a test server first. You may have to set a preference on your FTP client or server to show hidden files, as the “.” in front of the filename will generally cause the file to be hidden.
Attractive URLs
Now if you want to address our first example from above and make your totally square URL hip, you should have this in your htaccess file:
RewriteEngine on
RewriteBase /
Now, for each URL you want to beautify the directive looks like this:
RewriteRule ^home.html$ index.php?page=home
This says, if someone is asking for the page home.html, Apache will display the contents of index.php with the query string variable page equal to the value of home. This means you would direct people to home.html in links you are sending out.
Multiple Sites on One Server
Now, our second example involves having multiple websites on one server. To accomplish this you should have the separate folders or directories for each site. To make sure the right domain name goes to the right folder you’d put this in the htaccess file:
RewriteCond %{HTTP_HOST} ^www\.yourOTHERsite\.com
RewriteCond %{REQUEST_URI} !^/otherSite/
RewriteRule ^(.*)$ /otherSite/$1
This says, if someone comes here looking for yourOTHERsite.com, and they are not requesting a file that is already in the otherSite folder, you should direct their request for files to the otherSite folder.
Stay Out
Take back the web! Use the following line to exclude unfriendly IP addresses from your server (yes that’s a real spammer IP address):
order allow,deny
deny from 89.28.14.35
allow from all
To add more IP addresses just add another “deny from” line under the existing one.
So that’s a quick and dirty intro to .htaccess. I glossed over some things like the regular expression syntax used to match page URLs (all the ^, $, !, (.*) business) but that’s a whole other lesson entirely. If you need help bringing your site to a new level you can always contact sales@cgiinteractive.com
Anymore brain busters?