Reputation: 225
Can someone please explain to me what these .htaccess
rules mean?
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Upvotes: 0
Views: 174
Reputation: 5748
Let's do it step by step:
Options -MultiViews
A Multiviews search is enabled by the Multiviews Options. If the server receives a request for
/some/dir/foo
and/some/dir/foo
does not exist, then the server reads the directory looking for all files namedfoo.*
, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.
RewriteEngine On
The RewriteEngine directive enables or disables the runtime rewriting engine. If it is set to off this module does no runtime processing at all. It does not even update the
SCRIPT_URx
environment variables.Use this directive to disable rules in a particular context, rather than commenting out all the RewriteRule directives.
Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules.
RewriteRule ^(.*)/$ /$1 [L,R=301]
This rule will catch any request with a trailing slash, for example http://www.example.com/test/
.
In this request only the test
part will be catch because the trailing /
is outside the capture brackets ( )
.
Then it will redirect the user to http://www.example.com/
+ the catch string test
: http://www.example.com/test
. The [L]
flag means that no other rules will be checked. The [R=301]
flag means that the user will be redirected with a 301
code to this page.
So the purpose of this rule is to remove trailing slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This part is typically called a Dispatcher. Any request that is not pointing to a file (%{REQUEST_FILENAME} !-f
) or a directory (%{REQUEST_FILENAME} !-d
) will be sent to index.php
.
The purpose of this rule is to redirect rewritten queries like http://www.example.com/user/1/florian
(there is no folder user/1/
with a file named florian
on the filesystem) to index.php who will be able to handle this request.
Upvotes: 2