Join David Powers for an in-depth discussion in this video Working with a nonexistent directory, part of PHP: Clean URLs on Apache Server.
- [Voiceover] The third link in the navigation bar…of the example site loads a page called orchids.php.…We could rewrite the URL to point simply to orchids,…but this is a page specifically about caring for orchids,…so I think it would be better to include care in the URL.…One way would be to create a new directory called care,…but when rewriting URLs with mod_rewrite,…the underlying file structure doesn't need to match.…So let's go to .htaccess and add a new RewriteRule.…
So I begin with the RewriteRule directive,…and we need the incoming pattern:…We begin with a carat, then we're looking…for the literal text care/orchids.…That's all we want, so we need to finish…with the dollar sign.…We're not using a capturing group here,…all we need to do is to load the substitute page…which is orchids which is relative to this current directory…So orchids.php and then we finish with the flags…to make it not case sensitive and also to make…it the last rule if we've got a match.…
So that fixes that RewriteRule.…We need to create a clean URL in the navigation bar…
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