Someone Please Explain mod_rewrite Rules To Me! Part 2


What are the most common mistakes/pitfalls when writing rewrite rules?

Most problems with RewriteRule's stem from a misunderstanding of PCRE syntax/failure to properly escape special characters or a lack of insight into the content of the variable(s) used for matching.

Typical problems and recommended troubleshooting:

  • 500 - Internal Server Error - Remove Windows carriage controls in configuration file(s) if present, make sure mod_rewrite is enabled (wrap directives in IfModule conditional to avoid this scenario), check directive syntax, comment out directives until problem is identified
  • Redirect loop - Make use of RewriteLog and RewriteLogLevel, comment out directives until problem is identified

What is a good method for testing and verifying mod_rewrite rules?

First, look at the contents of the environment variable(s) you plan to match against - if you have PHP installed, this is as simple as adding the following block to your application:


... then write your rules (preferably for testing on a development server) and note any inconsistent matching or activity in your Apache ErrorLog file.

For more complex rules, use mod_rewrite's RewriteLog directive to log activity to a file and set RewriteLogLevel

Are there SEO or performance implications of mod_rewrite rules I should be aware of?

AllowOverride all impacts server performance as Apache must check for .htaccess files and parse directives with each request - if possible, keep all directives in the VirtualHost configuration for your site or enable .htaccess overrides only for the directories which need them.

Google's Webmaster Guidelines explicitly state: "Don't deceive your users or present different content to search engines than you display to users, which is commonly referred to as 'cloaking.'" - avoid creating mod_rewrite directives which filter for search engine robots.

Search engine robots prefer a 1:1 content:URI mapping (this is the basis for ranking links to content) - if you are using mod_rewrite to create temporary redirects or you are serving the same content under multiple URI's, consider specifying a canonical URI within your HTML documents.

Are there common situations where mod_rewrite might seem like the right tool for the job but isn't?

This is a huge (and potentially contentious) topic in its own right - better (IMHO) to address uses on a case-by-case basis and let askers determine whether the resolutions suggested are appropriate to their needs.

What are some common examples?

AskApache's mod_rewrite Tricks and Tips covers just about every common use-case that pops up regularly, however, the "correct" solution for a given user may depend upon the sophistication of the user's configuration and existing directives (which is why it is a generally a good idea to see which other directives a user has in place whenever a mod_rewrite question comes up).

Follow us @serversuit and like us on Facebook for the latest news and updates!

January 01 2020

Add or review comments

Please leave your comment

Existing comments

Comments 0