Un simplu fisier poate face multe pe un web server

E simplu. Cateva linii de cod si serverul se comporta cum vrei tu. In comentariile de mai jos in engleza, sunt descrise directivele de redirectare.


RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting or installed the project in a subdirectory,
# the base path will be prepended to allow proper resolution of
# the index.php file; it will work in non-aliased environments
# as well, providing a safe, one-size fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]

Am gasit descrierea de mai jos la adresa din stackoverflow

Suppose Zend is installed at http://mydomain.com/zend (let’s call it yourdomain later on) and we are requesting yourdomain/mycontroller/myaction

Therefore %{REQUEST_URI} will be “/zend/mycontroller/myaction”.

Note that $1, which is the pattern in the RewriteRule directive in the htaccess context [1], “will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. “app1/index.html” or “index.html” depending on where the directives are defined)”.

Therefore $1 will be “mycontroller/myaction”.

And %{REQUEST_URI}::$1 will be “/zend/mycontroller/myaction::mycontroller/myaction”.

The above string will be matched against ^(/.+)(.+)::\2$. Note that for the two capturing groups in round braces i.e., (/.+)(.+) before :: many combinations can match that. For example:

Group 1: /z

Group 2: end/mycontroller/myaction

or

Group 1: /zend/mycontroller/myactio

Group 2: n

and anything in between is a valid match. In fact, the most interesting one would be

Group 1: /zend/

Group 2: mycontroller/myaction

which (is the only case that) makes backreference \2 (after ::) to the second group a match.

In this case, “/zend/” will be stored in the environment variable BASE which is what the first RewriteRule does. The %1 refers to the first matched string in RewriteCond which is “/zend/”.

Looking at the second RewriteRule, it is clear that why there is a need for this. As index.php can only be found in /zend/index.php, we need to add “/zend/” in front of index.php.

Here we assume to use the URL-path as Substitution for the second RewriteRule directive. Refer to [1] and search for “A DocumentRoot-relative path to the resource to be served” under the RewriteRule Directive section.

All the above leave the query string unchanged/untouched. It is up to index.php how to parse the query string (as well as the URI).

Lastly goes the case where Zend is installed at the domain root.

%{REQUEST_URI} will be “/mycontroller/myaction”. $1 will be “mycontroller/myaction”.

The string to be matched by RewriteCond will be “/mycontroller/myaction::mycontroller/myaction”.

This time the second group in (/.+)(.+) will never match “mycontroller/myaction” as there needs to be at least one letter following the initial backslash for the first group, making the second group as close as “ycontroller/myaction” but not exactly “mycontroller/myaction” so there cannot be a match.

As a result, the first RewriteRule is not used. The BASE enviornment variable will not be set, and when the second RewriteRule uses it, it will simply be empty.

Referinta la parametrii tehnici se gaseste la adresa http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Cateva din variablile de server sunt:

Server-Variables: These are variables of
the form
%{ NAME_OF_VARIABLE
}

where NAME_OF_VARIABLE can be a string taken
from the following list:

HTTP headers: connection & request:
HTTP_ACCEPT
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_REFERER
HTTP_USER_AGENT
AUTH_TYPE
CONN_REMOTE_ADDR
CONTEXT_PREFIX
CONTEXT_DOCUMENT_ROOT
IPV6
PATH_INFO
QUERY_STRING
REMOTE_ADDR
REMOTE_HOST
REMOTE_IDENT
REMOTE_PORT
REMOTE_USER
REQUEST_METHOD
SCRIPT_FILENAME
server internals: date and time: specials:
DOCUMENT_ROOT
SCRIPT_GROUP
SCRIPT_USER
SERVER_ADDR
SERVER_ADMIN
SERVER_NAME
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME
API_VERSION
CONN_REMOTE_ADDR
HTTPS
IS_SUBREQ
REMOTE_ADDR
REQUEST_FILENAME
REQUEST_SCHEME
REQUEST_URI
THE_REQUEST

These variables all correspond to the similarly named HTTP MIME-headers, C variables of the Apache HTTP Server or struct tm fields of the Unix system.

La RewriteCond exista flag-uri:

You can perform various file attribute tests:

-d
Is directory.
Treats the TestString as a pathname and tests
whether or not it exists, and is a directory.
-f
Is regular file.

Treats the TestString as a pathname and tests
whether or not it exists, and is a regular file.

-F
Is existing file, via subrequest.
Checks whether or not TestString is a valid file,
accessible via all the server’s currently-configured
access controls for that path. This uses an internal
subrequest to do the check, so use it with care –
it can impact your server’s performance!
-h
Is symbolic link, bash convention.
See -l.
-l
Is symbolic link.
Treats the TestString as a pathname and tests
whether or not it exists, and is a symbolic link. May also
use the bash convention of -L or
-h if there’s a possibility of confusion
such as when using the -lt or
-le tests.
-L
Is symbolic link, bash convention.
See -l.
-s
Is regular file, with size.
Treats the TestString as a pathname and tests
whether or not it exists, and is a regular file with size greater
than zero.
-U

Is existing URL, via subrequest.
Checks whether or not TestString is a valid URL,
accessible via all the server’s currently-configured
access controls for that path. This uses an internal
subrequest to do the check, so use it with care –
it can impact your server’s performance!

This flag only returns information about things
like access control, authentication, and authorization. This flag
does not return information about the status code the
configured handler (static file, CGI, proxy, etc.) would have
returned.

-x
Has executable permissions.
Treats the TestString as a pathname and tests
whether or not it exists, and has executable permissions.
These permissions are determined according to
the underlying OS.

For example:

RewriteCond /var/www/%{REQUEST_URI} !-f
RewriteRule ^(.+) /other/archive/$1 [R]

La RewriteRule exista niste “flag”-uri si acestea sunt explicate mai jos.

Additionally you can set special actions to be performed by appending [flags] as the third argument to the RewriteRule directive. Flags is a comma-separated list, surround by square brackets, of any of the flags in the following table.

Flag and syntax Function
B Escape non-alphanumeric characters in backreferences before
applying the transformation.
backrefnoplus|BNP If backreferences are being escaped, spaces should be escaped to
%20 instead of +. Useful when the backreference will be used in the
path component rather than the query string.
chain|C Rule is chained to the following rule. If the rule fails,
the rule(s) chained to it will be skipped.
cookie|CO=NAME:VAL Sets a cookie in the client browser. Full syntax is:
CO=NAME:VAL:domain[:lifetime[:path[:secure[:httponly]]]]
discardpath|DPI Causes the PATH_INFO portion of the rewritten URI to be
discarded.
END Stop the rewriting process immediately and don’t apply any
more rules. Also prevents further execution of rewrite rules
in per-directory and .htaccess context. (Available in 2.3.9 and later)
env|E=[!]VAR[:VAL] Causes an environment variable VAR to be set (to the
value VAL if provided). The form !VAR causes
the environment variable VAR to be unset.
forbidden|F Returns a 403 FORBIDDEN response to the client browser.
gone|G Returns a 410 GONE response to the client browser.
Handler|H=Content-handler Causes the resulting URI to be sent to the specified
Content-handler for processing.
last|L Stop the rewriting process immediately and don’t apply any
more rules. Especially note caveats for per-directory and
.htaccess context (see also the END flag).
next|N Re-run the rewriting process, starting again with the first
rule, using the result of the ruleset so far as a starting
point.
nocase|NC Makes the pattern comparison case-insensitive.
noescape|NE Prevent mod_rewrite from applying hexcode escaping of
special characters in the result of the rewrite.
nosubreq|NS Causes a rule to be skipped if the current request is an
internal sub-request.
proxy|P Force the substitution URL to be internally sent as a proxy
request.
passthrough|PT Forces the resulting URI to be passed back to the URL
mapping engine for processing of other URI-to-filename
translators, such as Alias or
Redirect.
qsappend|QSA Appends any query string from the original request URL to
any query string created in the rewrite target.
qsdiscard|QSD Discard any query string attached to the incoming URI.
qslast|QSL Interpret the last (right-most) question mark as the query string
delimiter, instead of the first (left-most) as normally used.
Available in 2.4.19 and later.
redirect|R[=code] Forces an external redirect, optionally with the specified
HTTP status code.
skip|S=num Tells the rewriting engine to skip the next num
rules if the current rule matches.
type|T=MIME-type Force the MIME-type of the target file
to be the specified type.

Article by admin

Admin-ul e un fel de shef al site-ului. Se prespune ca stie ce face pe aici si ca ce produce, are o calitatea mai mare decat ce produc altii autori din site. Cred ca e opinia lui, una ce are invidia la baza, detinerea de informatii mai tari si mai multe decat altele detinute de ceilalti. Pana la urma categoriile din meniu definesc persoana prin domeniile puse acolo si prin scrierile din site. Persoana "Admin" a facut si a dres, are o experienta si poate fi catalogat prin ce stie, face si ce mai spune ca doreste sa faca in viitor. Un indiciu: e unamoid.

Comments: no replies

Join in: leave your comment

*