Описание директив файла .htaccess

Описание директив файла .htaccess проекта на Symfony

<IfModule mod_rewrite.c>
# This is an Apache configuration directive that designates this section
# is processed conditional on the presence of the mod_rewrite module
# being loaded.
# Simply, if mod_rewrite hasn't been installed and loaded, everything
# is skipped until the </IfModule>.
# Note, that just because this *section* is processed does not necessarily
# mean the *rewrites* will be processed. For that, we need to turn the...
RewriteEngine On
# . Now the rewrites will be processed.
# uncomment the following line, if you are having trouble
# getting no_script_name to work
RewriteBase /
# This directive explicitly sets the base URL for rewrites by stripping the
# local directory prefix before processing, and applying rules only to the
# remainder. Then, when processing is complete, the prefix is added back to
# the path. Here, all it does is strip the leading root slash, so a call to
# http://example.com/module/action/var/value
# will be processed by rewrites as
# module/action/var/value
# we skip all files with .something
# RewriteCond directives add a condition to which the next RewriteRule
# directive is subject. So, if and only if the following two RewriteCond
# directives match will the ".* - [L]" rule below them be used. Otherwise,
# if either RewriteCond directive does not match, the ".* - [L]" rule is
# skipped, and processing continues.
RewriteCond %{REQUEST_URI} \..+$
# This RewriteCond matches if there is one or more characters between a
# dot and the end of the request URI.
# Simply, this condition can be read as "match if the URI ends with a
# file name and extention."
# http://example.com/module/action/var/value would NOT match
# http://example.com/css/ would NOT match
# http://example.com/css/main.css WOULD match
# http://example.com/someSingleFile.html WOULD match, but then...
RewriteCond %{REQUEST_URI} !\.html$
# This RewriteCond matches if the request URI does not end with ".html".
# Simply, this condition can be read as "match if the URI does not end
# with 'a dot followed by the four characters h, t, m, and l'."
# http://example.com/module/action/var/value WOULD match
# http://example.com/css/main.css WOULD match
# http://example.com/someSingleFile.html would NOT match
# Plain HTML files are dealt with differently, so the combined effect of
# these two conditions is that they are skipped here and matched later
# to be processed by a different rule.
RewriteRule .* - [L]
# Combining the two conditions above, one could say this rule will only
# be used if the URI ends with a file name and extension, unless that
# extention is 'html'. This is why JavaScript and CSS files are accessible
# while the directory containing them is not:
# Because http://example.com/css/main.css statisfies both rules in that it
# - has a file name and extention, and
# - that extension is not 'html'
# However, http://example.com/css/ or http://example.com/css would not
# use this rule, because neither includes a file extension. This rule would
# be skipped, and processing of further rules would continue.
# In other news, this rule has the [L] flag set, which means that if this
# rule is used, the processing of rules stops, and Apache is directed to
# service the request with the URI as rewritten by the rule. In this case,
# that rule is to match everything (the pattern .* means "zero or more
# characters of any kind" which will match the entire URI) and pass it
# along unchanged (which is what the dash is for; it does nothing).
# we check if the .html version is here (caching)
# Remember how the last rule had an exception for files ending in ".html"?
# This is where those files are dealt with.
# Something of note here is that there are multiple RewriteRule directives
# followed by a single RewriteCond directive. In the group above, it was
# the other way around: one rule subject to several conditions. As long as
# the [L] flag isn't set, each rule does it's rewriting and just passes
# processing along. This way, the original URI can be massaged and prepared
# for any conditions or rules that might follow. This will make more sense
# in a bit.
RewriteRule ^$ index.html [QSA]
# This rule matches on an empty string, and then adds "index.html"
# Simply, this rule can be read as "if the URI is requesting the root
# directory, rewrite it so it is requesting the file 'index.html' in
# the root directory."
# This rule would turn
# http://example.com/
# into
# http://example.com/index.html
# Either way (whether this rule is used or not) processing continues to...
RewriteRule ^([^.]+)$ $1.html [QSA]
# which adds '.html' to the URI if it has no extention. Obviously, this
# means that this rule and the previous rule are mutually exclusive, since
# using the last rule rewrites the URI such that this rule will fail.
# (In fact, that rule does exactly what this rule does plus a little. That
# rule could be simplified to just "^$ index [QSA]", and then both rules
# would be used, and everything would function exactly the same, but that's
# not really here nor there. I'm just sayin'.) So, anyway....
# This rule turns
# http://example.com/foo
# into
# http://example.com/foo.html
# Literally, this rule could be read as "if there are one or more characters
# from the begining of the request to the end of the request that are not
# a dot, add a dot followed by the four characters h, t, m, and l" or
# more simply as "if there isn't a dot anywhere in the request, add '.html'
# to the end of it."
# At this point, the URI will be of one of three possible forms:
# http://example.com/index.html if the first rule matched,
# http://example.com/foo.html if the second rule matched, or
# http://example.com/foo.bar if neither rule matched.
RewriteCond %{REQUEST_FILENAME} !-f
# This takes the result after using the previous rules, and
# uses it to apply a condition on the following rule.
# "%{REQUEST_FILENAME}" tries to resolve the resulting URI to a location
# in the filesystem. The "-f" takes that location, and tests whether or not
# it exists, and if so, is a regular file. Recall that an exclamation point
# negates the test it precedes.
# So this can be read as "match if the processed URI does not correspond
# to a file on disk."
# no, so we redirect to our front web controller
# Hence, this rule will only be used if the rewritten URI does not
# resolve to a regular file on the file system.
RewriteRule ^(.*)$ index.php [QSA,L]
# This rule matches any non-empty URI (which by this point is a "can't happen"
# state, since the "^$ index.html [QSA]" rule made that impossible). So...
# This rule matches any URI that made it this far, and completely rewrites
# it to "index.php" alone, appends the query string ([QSA] flag), and stops
# processing any further rules ([L] flag).
# According to the previous RewriteCondition directives, the only way this
# rule would not be used is if the URI corresponds to a file on disk, in
# which case (since rewrite processing is stopped at this point) that file
# would be directly served up by Apache. Either way, service of this
# request is complete...
# big crash from our front web controller
# ...unless it isn't.
# If none of the above rules caused Apache to kick in and start serving the
# request by either fetching the file from disk or sending the request to
$ Symfony's front controller (including the case where the rules weren't
# processed because mod_rewrite.c wasn't loaded), then something is
# clearly very wrong, and the whole thing dies.
ErrorDocument 500 "<h2>Application error</h2>symfony application failed to start properly"

Вы можете оставить комментарий или подписаться на RSS feed


Как-то не солидняк! Можно было бы как минимум отформатировать нормально, а как максимум — перевести описания


Это кусок официальной доки? Читать тяжело. А на русском не было?

Leave a comment