Top 10 reasons your mod_rewrite rules don’t work
- You’re putting your rules in .htaccess but your config file has an affective value of
AllowOverride none for the directory.If you can put garbage in your htaccess file, and you don’t get a 500, it’s not being read by Apache.
- Leading slash in htaccess/per-directory contextIn htaccess or per-directory context, the base part of the URL is stripped before RewriteRule comparisons including a trailing slash. This means if you’re htaccess is sitting in directory
foo and you want to match a request for /foo/bar
- RewriteRule /foo/bar doesn’t match
- RewriteRule ^/bar doesn’t match
- RewriteRule /bar doesn’t match
- RewriteRule bar does match
- RewriteRule ^bar does match
- You’re trying to match against the Query String in a RewriteRuleYou need to use RewriteCond %{QUERY_STRING} before your RewriteRule, capturing as appropriate
- You’re trying to match against more then the “abs_path” of the URL (protocol, host, or port)Use a RewriteCond for %{HTTP_HOST} if you really need it to be host-specific
- You think the [L] flag prevents your rules from being run, but your rules are in htaccess/per-directory contextIn this context, your entire rule-set is re-run if you make any change, which may cause the new URL to not trigger the rule with the [L] flag a second time. Use more precise rules or conditions to guard against the re-entry.
- You tried to match a substring ‘abc’ with something enclosed in square brackets.Use parens.
- You embeded slashes before/after/around your regex that aren’t matched in the URL you care about
- You didn’t realize .* is both greedy (will match us match as possible) and will also match slashes.
- You used some convoluted character class to match “not a slash” instead of [^/]
- You only thought about how your initial request should be modified redirected, without any consideration for what should happen on subsequent requests. Or, when does your rule have to NOT run?
- NEW: You think your rules are only partially working, because MultiViews is enabled and doing half the stuff your non-matching RewriteRules are trying to do (foo->foo.php but you were also expecting query string side effects)