Fortunately, there are a number of good resources on the web, and a couple of books that help you understand all you need to know about .htaccess and mod_rewrite
-
Askpache is the most comprehensive site that I have found on .htaccess and mod_rewrite.
It covers everything from beginner to very advanced .htaccess use to SEO. There are a lot of examples that you can use as a start to writing your own .htaccess rules.
Click to view Table of Contents from AskApache.comHide.
AskApache Htaccess Journey
Htaccess – Evolved
What Is .htaccess
Creating Htaccess Files
Htaccess Scope
Htaccess File Syntax
mod_autoindex
mod_rewrite
Htaccess Directives
Litespeed Htaccess support
Main Server Config Examples
Example .htaccess Files
Example .htaccess Code Snippets
Redirect Everyone Except IP address to alternate page
When developing sites
Fix double-login prompt
Set Timezone of the Server (GMT)
Administrator Email for ErrorDocument
ServerSignature for ErrorDocument
Charset and Language headers
Disallow Script Execution
Deny Request Methods
Force “File Save As” Prompt
Show CGI Source Code
Serve all .pdf files on your site using .htaccess and mod_rewrite with the php script.
Rewrite to www
Rewrite to www dynamically
301 Redirect Old File
301 Redirect Entire Directory
Protecting your php.cgi
Set Cookie based on Request
Set Cookie with env variable
Custom ErrorDocuments
Implementing a Caching Scheme with .htaccess
Password Protect single file
Password Protect multiple files
Send Custom Headers
Blocking based on User-Agent Header
Blocking with RewriteCond
.htaccess for mod_php
.htaccess for php as cgi
Shell wrapper for custom php.ini
Add values from HTTP Headers
Stop hotlinking
Turn logging off for IP
Turn logging on for IP
Advanced Mod_Rewrite Examples
Directory Protection
Password Protect wp-login.php
Password Protect wp-admin
Protect wp-content
Protect wp-includes
Common Exploits
Stop Hotlinking
Safe Request Methods
Forbid Proxies
Real wp-comments-post.php
HTTP PROTOCOL
SPECIFY CHARACTERS
BAD Content Length
BAD Content Type
Missing HTTP_HOST
Bogus Graphics Exploit
No UserAgent, Not POST
No Referer, No Comment
Trackback Spam
Map all URIs except those corresponding to existing files to a handler
Map any request to a handler
And for CGI scripts:
Map URIs corresponding to existing files to a handler instead
Deny access if var=val contains the string foo.
Removing the Query String
Adding to the Query String
Rewriting For Certain Query Strings
Modifying the Query String
Best .htaccess Articles
.htaccess for Webmasters
Mod_Rewrite URL Rewriting
301 Redirects without mod_rewrite
Secure PHP with .htaccess
.htaccess Cookie Manipulation
.htaccess Caching
Password Protection and Authentication
Control HTTP Headers
Blocking Spam and bad Bots
PHP htaccess tips
HTTP to HTTPS Redirects with mod_rewrite
SSL in .htaccess
SetEnvIf and SetEnvIfNoCase in .htaccess
Site Security with .htaccess
.htaccess Security with MOD_SECURITY
Merging Notes
My Favorite .htaccess Links
Htaccess Directives
Htaccess Modules
Htaccess Software
Technical Look at .htaccess
Per-directory configuration structures
Command handling
Side notes — per-server configuration, virtual servers, etc.
-
Stupid Htaccess tricks is also a very comprehensive site that covers everything from beginner to advanced .htaccess use. It also has a lot of examples that are explained with simple language.
Click to view Table of Contents from Perishable Press Stupid htaccess TricksHide.
General
htaccess definition
htaccess comments
important information
performance issues
regex character definitions
redirection header codes
Essentials
htaccess comments
enable basic rewriting
enable symbolic links
enable AllowOverride
rename the htaccess file
retain httpd.conf rules
Performance
disable AllowOverride
pass the character set
preserve bandwidth
disable the server signature
set the server timezone
set admin email address
enable file caching
set default language & character set
declare specific/additional MIME types
send headers without meta tags
limit request methods to GET/PUT
process files according to request method
execute various file types via CGI script
Security
prevent access to htaccess
prevent access to any file
prevent access to multiple file types
prevent unauthorized directory browsing
change the default index page
disguise script extensions
limit access to the LAN
secure directories by IP or domain
deny/allow domain access for IP range
deny/allow multiple IP addresses on one line
miscellaneous rules for blocking/allowing
stop hotlinking, serve alternate content
block robots, rippers, and offline browsers
more stupid blocking tricks
even more scum-blocking tricks
password-protect directories
password-protect files, directories, and more
require SSL (secure sockets layer)
automatically CHMOD various file types
disguise all file extensions
limit file upload size
disable script execution
Usability
minimize CSS image flicker in IE6
deploy custom error pages
provide a universal error document
employ basic URL spelling check
force media downloads
display file source code
redirect visitors during site development
provide password-prompt during site development
prevent access during specified time periods
Redirects
important note about redirecting via mod_rewrite
redirect from www-domain to non-www-domain
redirect from an old domain to a new domain
redirect string variations to a specific address
other fantastic redirect tricks
send visitors to a subdomain
more fun with RewriteCond & RewriteRule
more fun with Redirect 301 & RedirectMatch 301
WordPress
secure wordPress contact forms
wordpress permalinks
Random
activate SSI for HTML/SHTML file types
grant CGI access in a specific directory
disable magic_quotes_gpc for PHP enabled servers
enable MD5 digests
expression engine tricks
-
Together with the Apache Mod-Rewrite Documentation, this is where most people eventually refer to for specific details. If you do not have a technical (developer, sysadmin etc) background, then you may want to approach one of the beginner guides first.
Click to view Table of Contents from Apache's rewriting guideHide.
Canonical URLs
Canonical Hostnames
Moved DocumentRoot
Trailing Slash Problem
Move Homedirs to Different Webserver
Search pages in more than one directory
Set Environment Variables According To URL Parts
Virtual User Hosts
Redirect Homedirs For Foreigners
Redirecting Anchors
Time-Dependent Rewriting
Backward Compatibility for YYYY to XXXX migration
Content Handling
Access Restriction
Other
-
This has introductory .htaccess material and is good for beginners.
Click to view Table of Contents from easymodrewrite.comHide.
Introduction
Before you begin
Start Rewriting
Extending Rules
Regular Expressions
Logging
Forcing www
Moved Pages (301)
Wildcard Subdomains
Force SSL (https://)
Prevent Hotlinking
File extension
-
This is also a good site for beginners, and it covers some common use cases.
Click to view Table of Contents from Htaccessguide.comHide.
What is .htaccess?
How to use .htaccess
Error documents
Redirects
Password protection
Deny visitors by IP address
Deny visitors by referrer
Hot link prevention techniques
Blocking offline browsers and ‘bad bots’
DirectoryIndex uses
Adding MIME types
Enable SSI with .htaccess
Enable CGI outside of the cgi-bin
Disable directory listings
Setting server timezone
Changing server signature
Preventing access to your PHP includes files
Prevent access to php.ini
Forcing scripts to display as source code
Ensuring media files are downloaded instead of played
Setting up Associations for Encoded Files
Preventing requests with invalid characters
-
This is less of a tutorial and more of a cookbook for specific htaccess needs.
Click to view Table of Contents from 21 htaccess tipsHide.
1. Custom Directory Index Files
2. Custom Error Pages
3. Control access at files & directory level
4. Modifying the Environment Variable
5. 301 Redirect using htaccess
6. Implementing a Caching Scheme with .htaccess
7. Compress output using GZIP
8. Redirect browser to https (ssl)
9. Rewrite URLs using htacccess
10. Prevent Directory Listing
11. Adding new MIME types
12. Deny access to static file data
13. Specify Upload file limit for PHP in htaccess
14. Disallow Script Execution
15. Change Charset and Language headers
16. Set Timezone of the Server (GMT)
17. Force “File Save As” Prompt
18. Protecting a single file
19. Set Cookie using htaccess
20. Send Custom Headers
21. Blocking request based on User-Agent Header
Other Web Resources
Htaccess cheat sheet with RewriteRule/rewriteCond flags, server variables etc. Assumes you already know .htaccess.
SEO 301 Redirect Examples gives you an SEO perspective on .htaccess.
Click to view Table of Contents from SEO 301 Redirect ExamplesHide.
SEO Benefits of 301 Redirects
Usability Benefits
Mod Rewrite Procedure & 301 Redirect Examples
301 redirect using Apache Mod Rewrite
Redirect Old pages to new pages with a simple 301 redirect
Redirect Folder (and Contents) to New URL
Canonical Domain Re-Writing
Dynamic URL Rewrite Example
Redirect All Pages to Another Domain
Image and Content Hotlinking – Bandwidth Theft
Wildcard Redirects
301 Redirect – URL’s With Spaces
Custom Error Page (404)
Testing 301 Redirects
View servervariables on a page to see how the URL maps to each one.
Server faults guide to mod-rewrite Covers some common examples of RewriteRule and RewriteCond
Excellent Regular expression guide This is the best online resource for learning regular expressions.
Books
Mastering Regular Expressions by Jeffrey Friedl – this is the best book to read if you want to fully master regular expressions and understand how the various regex engines work.
The Definitive Guide to Apache mod_rewrite: this book is a gentler introduction to mod_rewrite than the online Apache docs and also has plenty of examples.
Questions about .htaccess or mod_rewrite
If you have questions about .htaccess or mod_rewrite that are not covered in any of the resources listed, then you can post a question on Stackoverflow or Serverfault.
Know of other great .htaccess/mod_rewrite resources
If you know of other great .htaccess or mod_rewrite resources on the web, please let me know.
Tags: .htaccess, apache, mod_rewrite, seo