Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache RewriteRule, - (dash) as substitution

Tags:

I often see in PHP MVC applications an Apache RewriteRule that looks like this:

RewriteRule ^.*$ - [NC,L] 

The Apache docs for the RewriteRule directive states:

A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

So from what I gather, you can use this to transform a path using a flag i.e [NC] for the RewriteRules to follow?

Could someone please explain this dash RewriteRule a little better?

like image 792
Cobby Avatar asked Dec 03 '10 22:12

Cobby


People also ask

What is RewriteRule * F?

RewriteRule "\.exe" "-" [F] This example uses the "-" syntax for the rewrite target, which means that the requested URI is not modified. There's no reason to rewrite to another URI, if you're going to forbid the request.

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 is $1 RewriteRule?

In your rewrite, the ^ signifies the start of the string, the (. *) says to match anything, and the $ signifies the end of the string. So, basically, it's saying grab everything from the start to the end of the string and assign that value to $1.


Video Answer


2 Answers

See this answer:

mod_rewrite: what does this RewriteRule do?

It essentially means "do nothing if the previous RewriteConds match". The next RewriteRule(s) will instead do something else when the RewriteConds don't match. In the case of the post i linked you to, the next RewriteRule rewrites the url including "index.php".

like image 134
maxgalbu Avatar answered Sep 26 '22 07:09

maxgalbu


I know this is kinda old, but it basically says "Hey, i might wanna use Rewrite, but for urls that match the above RewriteCond(s), leave it be and stop."

EX:

# If file or directory exists, we wanna send them there RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule .* - [L]  # Otherwise, use our smart page handler! RewriteRule ^(.*)\.php$ virtualPage.php?page=$1 [L] 
like image 44
InsanityNet Avatar answered Sep 22 '22 07:09

InsanityNet