Join David Powers for an in-depth discussion in this video Replacing a query string with a numeric value, part of PHP: Clean URLs on Apache Server.
- [Voiceover] The first three links in the navigation bar…of the H and P example site now use clean URLs,…but if we scroll down in the arrangements page…and click one of the images at random,…it takes us to details.php with a query string appended…to the URL.…The query string contains a name value pair…that the script in details.php uses to query the database…and display the details of the selected arrangement.…We can rewrite the URL to get rid of the query string…and replace it simply with a number,…which will look more elegant and easier to remember,…so let's go to an editing program…and in .htaccess create a new RewriteRule,…so we start with the RewriteRule directive…and the incoming URL needs to match the literal text,…details at the start, followed by a /,…and that will be followed by the primary key…of the arrangement that we want to look for.…
We start with a carat, details/.…That's going to be followed by the primary key…of the selected arrangement, which we need to capture,…so a capturing group is in parentheses…
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