Using mod_rewrite for a Christmas frontpage

Posted: December 15th, 2012 | Author: | Filed under: .htaccess, Internet, mod_rewrite, Programming | No Comments »

I needed to add a quick splash to our homepage to give an update on our Christmas opening hours.

I added a quick jQuery pop-up to our index.php, then saved that as index_xmas.php.

Then it’s a simple matter of adding a line to our .htaccess file:

Redirect 302 /index.php http://www.domain.com/index_xmas.php

All requests to index.php are now sent to index_xmas.php.

The “Redirect 302” code tells the browser that the redirect is temporary and that it should continue to use the regular URL.

After Christmas all I have to do is remove that line from the .htaccess to resume normal service.


Converting MYSQL DATEs for PHP

Posted: July 30th, 2012 | Author: | Filed under: Internet, MYSQL, PHP, Programming | No Comments »

A site I am working on stores the date of new entries to the database by using MYSQL’s CURDATE() function. So if we’re adding a new entry with the make, model and date to the database, the SELECT query looks like this:

"INSERT INTO table_name (make, model, date) VALUES ($make, $model, CURDATE())"

This works fine, but when I then query the database, like so:

"SELECT make, model, date FROM table_name WHERE ..."

The date returned is in MYSQL’s date format, which is YYYY-MM-DD.
I was looking around for a quick and easy way to convert this in PHP to the UK DD-MM-YYYY format, and kept coming up with longwinded ideas for converting the MYSQL date to a UNIX timestamp, and then using all kinds of guff to format this as required.
It turns out the answer’s much simpler than that, if you use MYSQL’s DATE_FORMAT function. By passing in your date column and the format in which you want it presented you can create a new variable to query. So the previous query becomes:

"SELECT make, model DATE_FORMAT(date, '%d/%m/%Y') as formatted_date FROM table_name WHERE ..."

The %d/%m/%Y part is where you define how the date should be formatted. Dan Winchester has an excellent and comprehensive list of options on his website.

Now, instead of your PHP looking like:

$make = $row['make'];
$model = $row['model'];
$date = $row['date'];

it becomes:

$make = $row['make'];
$model = $row['model'];
$date = $row['formatted_date'];

thus returning your date information nicely pre-formatted by MYSQL.


Mod_rewrite on BT Business hosting

Posted: July 23rd, 2012 | Author: | Filed under: .htaccess, BT, Internet, mod_rewrite, Programming | No Comments »

I was banging my head earlier with a mod_rewrite problem. The site in question is to be hosted on BT Business hosting. My mod_rewrite rules were running perfectly on my local MAMP environment, and on my test environment at Dreamhost. But it kept crapping out at BT.

Solution – it turns out that BT’s hosting needs the RewriteBase condition filled in. In my situation I was running the test software in a directory called ‘test’ so I needed to add

RewriteBase /test

to my .htaccess file.

When I move it out of ‘test’, to the full domain, I will modify .htaccess to

RewriteBase /

Hope that saves you a bit of head-scratching!