Posts for tag: mod_rewrite 2

How to create Friendly URLs

What are Friendly URLs? Friendly URLs (sometimes know as tidy urls) are dynamic URLs (URLs with a query string attached) URLs that have been 're-written' to appear as static pages — usually retaining important keywords and information in them. An example would be a product page that contained a list of product information retrieved from a database, with a search facility to enable you to see a product category in more detail. For example, the main page showing all products might be:


http://www.example.com/products.php

and when showing a product category, it might be:


http://www.example.com/products.php?category=widgets&colour=blue

A re-written Friendly URL for the above might look something like this:


http://www.example.com/products-widgets_blue.html

So, how is it done? Well, there is more than one method to achieve Friendly URLs but the method I tend to use involves using .htaccess files. (note: this method will only work if you are using an Apache server with mod_rewrite enabled).

Create a text file (name it anything you like), open it and add the following lines:


Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^products-([^/\.]+)_([^/\.]+).html?$ products.php?category=$1&colour=$2 [L]

Friendly-URLs

Once you have created the text file, upload it to the root level of your site and then change its name to ".htaccess". (Note: htaccess files are invisible files, check first if there is one there already and if so, open it and copy & paste the text in to it).

The last thing you need to do is update any references to the URL in you html to use the new URL.

It's pretty easy to pick up the basics, however - to make it even easier I have created a simple web app for creating & .htaccess based Friendly URLs. Simply enter your exsisting full URL with query string, then make the appropriate choices to create your new URL and finally download your .htaccess file, ready to upload to your server.

Dynamic CSS - how to use PHP in your stylesheets

Q: Can you use PHP in your stylesheets?

A: yes, of course!

But why would you want to? Well, maybe you want your users to be able to control the font size or colour scheme on your site. Or maybe you need so supply and style individual IDs to dynamically generated content. Or maybe you want to change the header image depending on the time of day (like I do on this site). Whatever your reason it's very simple to achieve.

Perhaps the most simple method would be to include the styles in question in the <head> section of your HTML and manipulate them there, something like...


<style type="text/css">
<?php

## add you PHP here...

?>
</style>

...however, that's a bit untidy — if you want to move ALL your CSS, static & dynamic into one stylesheet, here's how...

1 Rename your style sheet to give it a .php extension eg: style.css → styles.php

2 This on its own will not work, you need to inform PHP that it is dealing with CSS, this is done by adding a PHP header to your stylesheet. Add the following to the top of your stylesheet.


<?php
header("Content-type: text/css");
?>

From now on you can add any PHP you require to your CSS file.

3 One more thing. You need to change the reference to your css file to make sure it is linking to the PHP file, something like:


<link rel="stylesheet" type="text/css" media="screen" href="css/styles.php" />

4 Or, if you prefer, and your site is hosted on an Apache server, you could use mod_rewrite to rewrite the url to do this, you would need to create a file called .htaccess* in the root level of your site containing the following code (if it already exsists, copy and paste the code into it):


Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^css/styles.css /css/styles.php [L]

this tells all calls to styles.css to access styles.php

* more on mod_rewrite & .htaccess files coming soon...

HOME