I think almost everyone would be confused about how to use RewriteBase and RewriteRule in .htaccess file. If you search google for the meanings of RewriteBase and RewriteRule, you will get a bunch of webpages talking about them. But I believe you won’t get clear answer about your question. By reading all the webpages in the search results, what you get in your mind would be something like:
RewriteRule includes a pattern to match the request url, if matched, RewriteRule replace the request with the substitution. Almost all tutorials and guides about Apache url rewrite module will give you links to the Apache documents such as http://httpd.apache.org/docs/current/mod/mod_rewrite.html and http://httpd.apache.org/docs/2.0/misc/rewriteguide.html, and say they are the best guides and most authoritative information on URL rewriting. But I believe you will still be confused after you finish reading those authoritative articles. Let’s first not answer your question about RewriteBase and RewriteRule, but analysis why the answers to the questions get so obscure.
Let’s find the answer to the question “what is matched against the pattern in a RewriteRule?” in the authoritative article(http://httpd.apache.org/docs/current/mod/mod_rewrite.html). Here is its answer:
What is matched?
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. “app1/index.html” or “index.html” depending on where the directives are defined).
What do get from the answer? Do you know now what on earth is matched against? filesystem path? The pattern removes a prefix? The pattern removes a prefix of filesystem path? Something led the server to the current RewriteRule(what is it?)? e.g. “app1/index.html” is an example for what? what “depends on where the directives are defined”? I am really confused about all it talks about. And I get an illusion that something active can lead the server to the current RewriteRule.
The fact is actually very simple: If you type a url in your browser: http://localhost/domainhostseotool/dir/file.php, and you put the .htaccess in dir/, the pattern is matched again “file.php”, no protocol and host part of the request url, no paths to the current dir(the directory that the .htaccess resides). If your request url is http://localhost/domainhostseotool/dir/subdir/file.php and the .htccess is still put in dir/, the pattern is matched against “subdir/file.php”. Do you understand what is matched against now?
I am curious why such a simple fact can be hidden in so much information junk on the Internet.
Let’s examine how the RewriteBase is explained in that authoritative artitcle:
The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.
This directive is required when you use a relative path in a substitution in per-directory (htaccess) context unless either of the following conditions are true:
What is a URL prefix? How the URL prefix is used? what is a relative path?
By reading the RewriteBase section of this article, I cannot understand anything about RewriteBase.
However the fact is also very simple.
If a RewriteRule successfully matches its pattern again the part of url(which we just talked about), the substitution part of the RewriteRule will be used to get the final(real) url. If the substitution begins with a slash(“/”), it is considered as a absolute path which will be appended to the protocol(http://) and host part of the request url to form the final url. So if the RewriteRule(in .htaccess in dir/) is:
RewriteRule .+ /index.php [L]
and your request url is http://domainhostseotool.com/dir/test.php, the final url is: http://domainhostseotool.com/index.php
If the substitution part of the RewriteRule does not begin with a slash, it is called relative path. A relative path will be pre-pended by the RewriteBase and then the result is prepended by the protocol and host part of the request url to form the final url. So if your RewriteRule is changed to:
RewriteRule .+ index.php [L]
Your request url http://domainhostseotool.com/dir/test.php will be rewritten to:
http://domainhostseotool.com/dir/index.php. This is the why we use RewriteBase, because our website files are usually in a directory, and we may put the directory anywhere (under the web root, or under some subdirectory in the web root), to avoid changing the .htaccess file too much, we use relative path for the RewriteRule’s substitution and set the RewriteBase to the current path of the directory of our web files.