Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the $1 argument in this RewriteCond do?

Tags:

I'm setting up Apache rewrite rules to tidy up my CodeIgniter URLs.

This question (and lots of forum posts etc that I've found around the place) document the use of the following rule (or something very similar):

RewriteEngine on RewriteCond $1 !^(index\.php|phpinfo\.php|images|robots\.txt|sitemap\.xml\.gz|sitemap\.xml|assets) RewriteRule ^(.*)$ /index.php/$1 [L] 

I know the $1 after the RewriteRule refers to the captured string from (.*), but what does the first $1 (straight after the RewriteCond) represent? A lot of examples I've seen use something like %{REQUEST_URI} as the first argument for RewriteCond.

like image 788
Highly Irregular Avatar asked Nov 13 '12 21:11

Highly Irregular


People also ask

What is RewriteCond in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is in RewriteRule?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

What does QSA mean in htaccess?

QSA = Query String Append. This rule appends the GET query string which results from the ReWrite rule to the initial GET query string sent by the browser. For example, take the following RewriteRule: RewriteRule ^/product/([0-9]*)/? /

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.


1 Answers

The $1 is basically the captured contents of everything from the start and the end of the string. In other words, $1 = (.*).

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.

So if I type in www.example.com/tacos-are-good, then $1 = "tacos-are-good". So your end rewrite will actually be www.example.com/index.php/tacos-are-good.

Here's a cheat sheet for ModRewrite which may be of help: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/

like image 88
sbeliv01 Avatar answered Sep 17 '22 22:09

sbeliv01