Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteRule in htaccess

Could anyone explain the following line please?

RewriteRule ^(.*)$ /index.php/$1 [L]
like image 211
shin Avatar asked Dec 23 '22 01:12

shin


2 Answers

The parts of the rewrite rule break down as follows:

  1. RewriteRule
    Indicates this line will be a rewrite rule, as opposed to a rewrite condition or one of the other rewrite engine directives

  2. ^(.*)$
    Matches all characters (.*) from the beggining ^ to the end $ of the request

  3. /index.php/$1
    The request will be re-written with the data matched by (.*) in the previous example being substituted for $1.

  4. [L]
    This tells mod_rewrite that if the pattern in step 2 matches, apply this rule as the "Last" rule, and don't apply anymore.

The mod_rewrite documentation is really comprehensive, but admittedly a lot to wade through to decode such a simple example.

The net effect is that all requests will be routed through index.php, a pattern seen in many model-view-controller implementations for PHP. index.php can examine the requested URL segments (and potentially whether the request was made via GET or POST) and use this information to dynamically invoke a certain script, without the location of that script having to match the directory structure implied by the request URI.

For example, /users/john/files/index might invoke the function index('john') in a file called user_files.php stored in a scripts directory. Without mod_rewrite, the more traditional URL would probably use an arguably less readable query string and invoke the file directly: /user_files.php?action=index&user=john.

like image 109
meagar Avatar answered Dec 28 '22 09:12

meagar


That will cause every request to be handled by index.php, which can extract the actual request from $_SERVER['REQUEST_URI']

So, a request for /foo/bar will be rewritten as /index.php/foo/bar

like image 27
Paul Dixon Avatar answered Dec 28 '22 11:12

Paul Dixon