Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite: what does this RewriteRule do?

Tags:

mod-rewrite

Given these conditions (I know what they mean/do):

RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d  

What does the first rule do? What is that lonely dash for?

RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ /index.php [NC,L] 

I've been using this for quite some time now in combination with the Zend Framework, but I never really got what the first rule does exactly.

like image 500
Decent Dabbler Avatar asked Jan 20 '10 14:01

Decent Dabbler


People also ask

What is Apache RewriteRule?

RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.

What does IfModule mod_rewrite C mean?

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.

How do I know if mod_rewrite is enabled?

Open any web browser browser and type following the URL, 'localhost/check. php'. It will display the PHP version details and Apache Configuration. In Apache Configuration, search for the Loaded Modules section, and there you will find all the modules that are enabled.


1 Answers

The RewriteCond directive just describes an additional condition for a RewriteRule directive. So RewriteCond must always be associated with a RewriteRule.

In your case the three RewriteCond probably belong to the first RewriteRule like this:

RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] 

Now this rule is applied if the pattern of the RewriteRule matches the current request URL (per-directory path stripped before) and if the condition is fulfilled.

In this case the condition is only true if when mapping the request URL to the filesystem it matches either an existing file with the file size greater than 0 (-s), or a symbolic link (-l) or a directory (-d). So your rule will be applied for any URL (^.*$ matches anything) that can be mapped to something existing in your filesystem. The substitution - just means to not change anything. And the NC (no case, case insensitive, useless in this context) and L (last rule if applied) are flags that modify either the pattern, replacement or the execution of the rule.

like image 180
Gumbo Avatar answered Sep 19 '22 11:09

Gumbo