views:

189

answers:

2

What does that rewrite rule mean?

   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]

Sorry, even after reading the mod_rewrite syntax explanation, I didn't get it... :-/ Can someone explain me what that rule(s) does.

Thanks a lot.

+4  A: 

I'll try to give a brief explanation that hopefully clears this up for you.

First: If you've never used Regular Expressions, then go look it up.

A RewriteRule is three different pieces. First is the expression used to search a request string for a particular pattern. Second is the destination string that is actually served up from the server. Third are the options which go inside the brackets.

If a client makes a request to the server in the format of one of the regular expressions, then the destination string is actually called and returned to the user, though the user does not see this action. Anything in your regular expression that is inside of a set of parentheses will be saved for use in the destination string.

So, with your two rules (assuming your site is http://www.example.com/), the following would happen:

I input http://www.example.com and the server returns the index page or the filelisting (or something else already determined by a previous .htaccess file).

I input http://www.example.com/page2.html and the server would return app/webroot/page2.html. If that file doesn't exist, then I would get a 404 Error Page instead.

BOTH of the RewriteRules have [L] at the end of them. What this does is it tells Apache that this is the Last rule to follow. If the rule fits, Apache will run it and then stop reading further rules. There are other options you can use, separated by commas.

Jeff Rupert
If there's anything in particular I said that confuses you, let me know and I'll try to fix it.
Jeff Rupert
Thanks a lot for the explanation. The second rule ( RewriteRule (.*) app/webroot/$1 [L] ) makes total sense to me now after reading your explanation.But what does the first rule do? (...especially the "^$" within the first rule seems strange to me).Thanks again
Jennifer Weinberg
Seth explains it very well. ^ is defined as 'beginning of string' in a regular expression, and $ is defined as 'end of string.' In this case, you have '^$' which evaluates to an empty string. So, the server will evaluate to ./app/webroot/ because it's not looking for a file. Due to the way Apache handles those "empty" requests, it can return anything I mentioned. Also, it will append '/' because webroot is a directory.
Jeff Rupert
@Jeff Rupert, wish I could double up-vote you. Very well written! Welcome to stackoverflow!
zen
@zen: Thank you! Finally decided to stop trolling and start being active. =D
Jeff Rupert
A: 

^$ catches requests to / and redirects them to app/webroot/. The [L] indicates that this is the "last" rule, meaning that mod_rewrite will stop processing if this rule matches the request.

(.*) matches anything, and redirects it to app/webroot/, and appends the "anything" part. Again the [L] means stop processing after this rule has fired.

The first rule would catch requests for http://www.yoursite.com/, sending them to http://www.yoursite.com/app/webroot/ (your webserver would then serve up the index document, such as index.php to satisfy the request.

The second rule would catch any other request, such as http;//www.yoursite.com**/path/to/some/page.php**. The bold part is "captured" and mod_rewrite will send the request to http://www.yoursite.com/app/webroot/path/to/some/page.php.

The user would only see http://www.yoursite.com/.

Also note that this expression probably will cause a problem. If you type in http://www.yoursite.com/app/webroot/index.php, mod_rewrite will change the request to http://www.yoursite.com/app/webroot/app/webroot/index.php, which is probably not what you want.

Seth
thanks a lot seth
Jennifer Weinberg