Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my RewriteRule via .htaccess? (Why does it need RewriteBase?)

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

not working,but

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

works.

like image 831
user198729 Avatar asked Jan 26 '10 04:01

user198729


People also ask

What is RewriteBase in htaccess?

What is RewriteBase. RewriteBase directive allows you to easily set the beginning of all relative paths used in . htaccess file. Its value is used in the target/destination path mentioned in rewrite rules in . htaccess file.

What is RewriteBase Apache?

RewriteBase allows you to adjust the path that mod_rewrite automatically prefixes to the result of a RewriteRule . A rewrite within the context of . htaccess is done relative to the directory containing that . htaccess file. The immediate result of the RewriteRule is still relative to the directory containing the .

What is nc l in htaccess?

L (last - stop processing rules) Last rule: instructs the server to stop rewriting after the preceding directive is processed. N (next - continue processing rules) NC (case insensitive) NE (do not escape special URL characters in output)

What is IfModule Mod_rewrite C?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.


2 Answers

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.

like image 130
outis Avatar answered Nov 15 '22 07:11

outis


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]
like image 28
Abel Avatar answered Nov 15 '22 07:11

Abel