Category Archives: Apache

The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows NT. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards.

Apache has been the most popular web server on the Internet since April 1996. The November 2005 Netcraft Web Server Survey found that more than 70% of the web sites on the Internet are using Apache, thus making it more widely used than all other web servers combined.

URL Rewriting Clean Folders

Here are a couple lines I like to use on sites to force the www subdomain throughout the site and also keep folders clean – meaning no /folder/index.html or /folder/index.php, instead they automatically go to /folder/. These techniques involve a 301 redirect so all your link juice is easily transferred and you avoid any duplicate content penalties.

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

RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html|htm)\ HTTP
RewriteRule ^(.*)index\.(php|html|htm)$ /$1 [R=301,L]

Parse XML Files as PHP

Here is a simple apache trick to parse xml files as php by adding a line to your htaccess file. You could add other extensions as well that you wanted to parse as php. An example when you would want to use this is for a dynamic xml file.

AddType application/x-httpd-php .php .xml

Note: The application type may differ slightly depending on your server configuration of php

Apache Mod Rewrite

One if the cleanest ways to setup a site is to use mod_rewrite in Apache. I am going to show you a simple way to do this using an htaccess file and a little PHP. First step is to make an htaccess file with the following code:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?loc=$1

Now everything that is not a folder or a file will get passed to the index.php file as $_GET['loc']. You could change the index.php to any file you like and the ?loc= to some other variable if you like.

Next you need to groom the url. Do not leave out this step. You are injecting the url into your script… I suggest something solid like the following:

<?php
$url = preg_replace('/[^[:alnum:]\-\/]/', '', $_GET['loc']);
?>

So now you have the url into your script safely so you need to break it apart. You are basically going to pass variables here. All you have to do is explode on the forward slash. You can do it simply like this:

<?php
$parts = explode('/',$url);
$section = $parts[0];
$subsection = $parts[1];
?>

301 Redirect

301 redirect is the most efficient, spider/visitor SEO friendly method for webpage redirection for web servers running apache. It is easy to implement and it should preserve your search engine rankings for that particular page. The code “301″ is interpreted as “moved permanently”. If you have to change file names or move pages around, it’s the safest option.

You can do a 301 redirect with PHP as follows:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.bradino.com");
?>

You can also do this in Apache using an htaccess file as follows:

Simple create a .htaccess file and put the follwoing entries:

Redirect 301 /pro http://bradino.com/php
Redirect 301 /web/portfolio http://bradino.com/php