6
rewriteengine on rewriterule ^/a/b$ ^/c$ 

not working,but

rewriteengine on rewritebase / rewriterule ^a/b$ ^c$ 

works.

1
  • 1
    @unknown-- this is a good question. If you don't get anyone to bite here, you might try moving it to serverfault.com. I also encourage you to accept answers to more of your questions and possibly even answer some questions along the way, too... Commented Jan 26, 2010 at 4:21

2 Answers 2

11

It's probably not the RewriteBase that makes the rule work so much as the leading slash. Also, the second argument to RewriteRule isn't a regular expression. Instead, try:

RewriteRule ^/?a/b$ c 

When applying a RewriteRule from .htaccess, the leading slash will be stripped from the URL, which will cause the pattern to fail if you include it. By starting a pattern with "^/?", it will work in the main configuration files and in per-directory config files.

Read over the detailed mod_rewrite documentation for the details of how the rewrite engine works and the significance of RewriteBase.

Edit: As mentioned in the mod_rewrite technical details and described in the documentation for RewriteRule and RewriteBase, the URL has been translated to a path by the time the per-directory rewrite rules are evaluated. The rewrite engine no longer has a URL to work with. Instead, it removes the local directory prefix (the directory holding the .htaccess file), which ends with a slash. For example, suppose a visitor requests "/var/www/foo/bar/baz.html" and there is a rewrite rule set in "/var/www/foo/.htaccess". Fore each rule, the rewrite engine will strip "/var/www/foo/", leaving "bar/baz.html" to be matched against the rewrite rule. After processing a rule, the local directory prefix is prepended (unless the replacement begins with "http://"). After all the rewriting rules have been processed, the rewrite base, if set, replaces the local directory prefix; if not, the document root is stripped. The rewritten URL is then re-injected as a sub-request.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you elaborate why the leading slash is stripped when applied from .htaccess?
1

What version of Apache are you using? RewriteBase should not be necessary when you are rewriting from the root. If you are not, you may need it. For instance, a part of my current configurations (Apache 2.2) for one of my blogs looks as follows, and works:

RewriteEngine On RewriteRule ^/$ /blog/ [R] RewriteRule ^/wordpress/(.*) /blog/$1 [R] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.