How to enable mod_rewrite in Apache

June 22nd, 2008

To enable the rewrite module in Apache you will need access to the httpd.conf file, located in {Apache folder}/conf/httpd.conf
Open up the file, and search for
#LoadModule rewrite_module modules/mod_rewrite.so
and simply remove the # from the start of the line. Then save the file, and restart the Apache server. You should now be able to use the mod_rewrite functionality on your server. to test this, create a new file called test.php in your root html folder, with the following code in it

and put

RewriteEngine On
RewriteBase /
RewriteRule ^test/(.+)$ test.php?p=$1 [L]

into a .htaccess file in the same folder

Don’t forget to delete these two files after testing

Headers already sent error php solution

June 22nd, 2008

Many people seem to get a problem with the common error message of

Warning: Cannot modify header information - headers already sent by (output started at C:\file.php:1) in C:\file.php on line 4)

There are a number of possible reasons for why this might happen

Output before the <?php tag

There are so many occasions where people report that they have no output, only to find that they have a single space or a new line before their opening <?php tag. Examples

 

appears to be fine to the untrained eye, but upon further inspection we notice that their is a slight gap (one space) before the <?php tag.


also looks fine, however a single carriage return still gets output to the browser, so it's still going to cause the same problem

Output inside your script via functions

php has a number of functions that display code, such as echo(), var_dump() and print_r(). Some people also believe that (X)HTML code does not qualify as content for some reason, and leave it there. This is STILL output, and will still bring up the error. Another instance where people get confused is where there is more than one error on the page. For instance if a Notice: error message comes up before the headers are sent, this will also be classed as an output, therefore turn off notice messages (This should be the case anyway for production servers)

The error is caused by a different file

Warning: Cannot modify header information - headers already sent by (output started at /home/username/www/includedfile.php:1) in /home/username/www/file.php on line 4)
Above shows the location of the file where the output was started at, and also, the line on which it started. Simply go to that line with your editor in that file, and work out what is causing the output. This type of error is common when using include/include_once or require/require_once

When including php from other files, it is a good idea to not have the closing ?> tag. This will NOT affect your script in any way, as PHP is quite happy with no tag. It will also prevent output being left after the tag such as the single space/new line as mentioned above. This is good practice and I recommend doing this with any included files with php, since you should not mix both (X)HTML and PHP in includes other than templates

Making dynamic/pretty URL’s for PHP scripts

June 20th, 2008

We’ve all seen sites where the URL looks really ugly, i.e.
example.com/product.php?id=12345&action=purchase&num=2

However, over the last few years, more and more sites have converted to dynamic/pretty URL’s, where it looks more like example.com/buy/12345/2/

Using some simple regular expressions and the rewrite module of apache you can create pretty page URL’s

So we have three sections, clearly coloured below:
example.com/buy/12345/2/ need to map to
example.com/product.php?id=12345&action=purchase&num=2

Here’s the RewriteRule for this example, which should be put inside your .htaccess file

RewriteRule ^buy/(\d+)/(\d\d?)/?$ /path/to/public_html/product.php?id=$1&action=purchase&num=$2 [L]

This is a case sensitive search, ie, /BUY/ would not work. If you wish to make it case insensitive, change the [L] to [L,NC]
Note: If you haven’t already got

RewriteEngine on
RewriteBase /

in the top of your .htaccess file, you will need to add it. The first line is required to get the rewrite engine working, and the second is to start matching the URL after the / on the end of example.com/

how to add www to your domain name using .htaccess file

June 19th, 2008

If you are wanting to add www. to your domain name automatically such as changing http://yourdomain.com to http://www.yourdomain.com then you can use a few simple lines in your .htaccess file in your public_html folder. This is especially useful for SEO as search engines index the domain with and without the www. as seperate sites.

Note that you will need the mod rewrite plugin active in your apache configuration in order to do this

#Turn on rewriting engine
RewriteEngine on
# If HTTP_HOST starts with yourdomain.com
# (Note: In a regular expression you have to escape the .
# with the backslash)
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
# change the URL to the URL with www prepended and
# use a permanent redirect
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

################################################
# ALL LINES STARTING WITH A # ARE COMMENTS,    #
# AND SHOULD BE REMOVED IN YOUR VERSION        #
#                                              #
# LEAVING THEM IN BETWEEN THE RewriteCond and  #
# RewriteRule LINE WILL CAUSE THE FILE TO      #
# FUNCTION INCORRECTLY                         #
################################################

.htaccess file generator

Enter your domain name below without the www like yourdomain.com and click submit to generate your htaccess file