I was wondering what does the following mod_rewrite code do, can someone explain the code in detail?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Line by line interpretation:
Will be interpreted first and match every request (matched against the part of the URL after the hostname and port, and before the query string).
.*is a regular expression to match all the time. A better and faster rule might be:^in regex means, match every string that has a beginning. This is equal to.*-> match every thing.After a match was found, the processing will continue with the
RewriteCond(itions).The two RewriteConditions are chained by a invisible logic
AND. This block will only match if both RewriteConditions are true.Example: If you’ll have the following file structure on the server.
If you request the URL example.com/css/base.css the following steps will happen.
RewriteCond %{REQUEST_FILENAME} !-f, becausecss/base.cssis a file.RewriteRulewill be skipped because no match occurred, processing will test other rules and conditions*If you request the URL example.com/en/about the following steps will happen.
RewriteRule(match all the time)RewriteCond %{REQUEST_FILENAME} !-f, becauseen/aboutis no file.RewriteCond %{REQUEST_FILENAME} !-d, becauseen/aboutis no directory.RewriteRuleandRewriteCondcreated a positive match ->index.phpwith the flagLThe flag
Lmeans last rule, which will stop the processing. More on flags.This rule combination is often used to redirect all request to an single entry point of an web application. To avoid serving static content by
index.php, files and directories will be served by web server and not by the index.php. The dispatching of the dynamic site request will be done inside the logic ofindex.php.* The correct data flow can be found here