Join David Powers for an in-depth discussion in this video Using the exercise files, part of PHP: Clean URLs on Apache Server.
- [Voiceover] The best way to learn how to create clean URLs is to work on an example site in your local testing environment. So the exercise files for this course focus on several pages of this site for a fictitious flower shop. Most pages query a database, so if I scroll down in this page and click one of the images, it loads another page with a query string appended to the URL. All the necessary files are supplied in exercise files that accompany this course. If you're a member of Lynda.com, download them from the course page and unzip them to a convenient location on your computer.
I've got them here on my desktop, so let's take a look inside. There are four folders, one for each chapter and the last folder, handp, contains the example site we'll be working with. Let's take a look inside this last folder. There are five webpages, and the includes folder contains the database connection script and the header and footer for each webpage. Creating clean URLs involves editing internal links in the webpages. So for most of chapters two and three, you'll be working exclusively in the handp folder.
So what are in the chapter-related folders? Well, chapter one is devoted to setting up the testing environment. The folders for the first two videos contain a script that tests whether your server is correctly configured to create clean URLs, and the last folder in chapter one contains an SQL file to set up the database for the handp site. The chapter two and three folders contain subfolders for each video that uses exercise files.
Let's take a look inside this one. Each one contains a begin and an end folder, and is in this particular case there's also script.txt which contains a snippet of code to be used in the video. The begin and end folders contain most of the files from the handp site as they look at the beginning and end of each video. If we take a look inside the begin folder, you'll notice that it doesn't include the images and styles folder, because they never change, so they're not included.
Because most files use internal links relative to the site root, you can't load the example site directly from the chapter-related folders. Always use the handp folder. But if you need to update the example site for a particular video, copy all of the files from the begin folder for that video and use them to replace the files in the handp folder. Because we're working with a PHP site, all of the exercise files need to be inside your testing server root. I created a folder called rewrite at the top level of my local host root.
I suggest you do the same. If you use a different location, you'll need to adjust the site root relative links in the exercise files. By the end of the course, the site will have been converted to use clean URLs, so if I scroll down now in this page and click one of the images, it's loaded with a clean URL, details/brighten-any-day and the same applies to other pages. So if I click Fresh Flowers, the URL is handp/flowers.
There's no dot PHP on the end. Incidentally, if you followed my PHP for web designers course, you'll recognize the design of the hansel & petal site, however, the underlying structure has changed. This version is more database driven. It also uses CSS that won't work in older browsers, so do make sure that you're using an up-to-date browser.
Rather than checking URLs individually, David shows how to evaluate and update them sitewide with the Apache mod_rewrite module. Watch these tutorials to learn how to enable mod_rewrite on your server, understand the structure of mod_rewrite rules, remove unnecessary file extensions, convert query strings into readable numbers and text, and use rewrite maps to look up complex values without regular expressions. Plus, learn how to enhance search results and provide searchers with links to specific parts of a webpage.
- Checking your Apache setup
- Creating simple regular expressions
- Removing .php from URLs
- Replacing query strings
- Displaying clean URLs in search results
- Linking to parts of a webpage
- Creating a clean URL for any PHP file