You know those fantastic ‘SEO friendly URLs’ you see on Drupal or WordPress sites? Well, you can use them in bespoke sites too…
I used this recently for The Wedding Industry Awards site where I wanted to use a single PHP page to include different display scripts depending on ‘how many’ variables were passed to it. In my case, it was region, category and company name.
So if nothing was passed it’d display a map of the UK with links to the different regions.
If a region was passed it’d display a list of categories within that region.
If a region and category were passed it’d display a list of companies in that category, in that region.
And finally, if a company name, category and region were passed, it’s display the company profile.
Still with me? Here’s an example.
Page with nothing passed: http://www.the-wedding-industry-awards.co.uk/find-a-supplier/
Page with region passed: http://www.the-wedding-industry-awards.co.uk/find-a-supplier/london-and-south-east
Page with region and category passed: http://www.the-wedding-industry-awards.co.uk/find-a-supplier/london-and-south-east/photographer
Page with everything passed: http://www.the-wedding-industry-awards.co.uk/find-a-supplier/london-and-south-east/photographer/breathe-pictures
So, how is it done? Thanks to Mark Jackson‘s brilliant method…
In the .htaccess file….
# Send all pages with /find-a-supplier/ in the URL to the vote.php page for processing RewriteRule ^find-a-supplier/(.*)$ /find.php?req=$1 [L,QSA]
In my find.php file….
// Break up URL if(isset($_GET['req'])){ $requests = explode('/',trim($_GET['req'])); } // Set URL vars if(isset($requests[0])) { $region = purifyURL($requests[0]); } else { $region = FALSE; } if(isset($requests[1])) { $category = purifyURL($requests[1]); } else { $category = FALSE; } if(isset($requests[2])) { $company = purifyURL($requests[2]); } else { $company = FALSE; }
The purifyURL() function is here…