How does this mod_rewrite code resolve properly?

.htaccess, apache, php

Solution

The particular behavior you're asking about comes about because the rule

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

is a 301 redirect; it instructs the browser to initiate a completely new HTTP request. The `L` only causes (can only cause) it to be the last rule executed for that request; the new request comes in with the correct hostname and proceeds onward.

Problem

My .htaccess file is as follows: ``` Options -Multiviews RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ $1.php ``` It works, but I'm wondering how it works. For example, if I type in `example.com/main`, I get the file at `www.example.com/main.php`. How do I get the `.php` extension if the code tells the rewriting to stop after adding the `www.` to the beginning of `example.com`? Edit: Or should I create a unique ID only for the purpose of logging in the remembered user?

Original source