Zend Framework settings (apache/rewrite rule)

Source: Internet
Author: User
Tags nginx server

This article introduces a simple set of Zend Framework (apache/rewrite rule). If you need it, refer to the examples.

Edit the C: WINDOWSSystem32driversetchosts file and add a domain, such as 127.0.0.1 audit. local.
Modify the apache configure file. In the xampp environment, edit httpd in the Directory d:/xampp/apache/conf. conf, enable rewrite module: Remove this line of comment: LoadModule rewrite_module modules/mod_rewrite.so then edit the extra/http-vhosts.conf file and add the following lines:
<VirtualHost *: 80>
DocumentRoot "D:/xampp/htdocs /"
ServerName localhost
ErrorLog logs/localhost. error_log
CustomLog logs/localhost. access_log common
</VirtualHost>
<VirtualHost *: 80>
DocumentRoot "D:/xampp/audit/public /"
ServerName audit. local
ErrorLog logs/audit. error_log
CustomLog logs/audit. access_log common

RewriteEngine off
<Location/>
RewriteEngine on
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule !. (Js | ico | gif | jpg | jpeg | pdf | png | css) $/index. php
</Location>
<Directory "D:/xampp/audit/public">
# Allow server side include, cgi, disable directory index
Options FollowSymLinks between des ExecCGI
Order allow, deny
Allow from all
</Directory>
</VirtualHost>
Remove the annotation of the line NameVirtualHost and restart apache to start using it.


For more information about the apache rewrite rule Writing Method and description.

Rewrite engine parses each rewrite rule. Each rewrite rule can contain or does not contain rewrite condition. If the rewrite rules match, the rewrite condition will be further checked. The specific process is as follows:

First, match the patern of rewrite. If not, enter the next rewrite rule.

If yes, mod_rewrite checks rewrite condition. If no condition exists, the new string replaces the url and then enters the next rewrite rule.

If rewrite condition exists, check conditions in order. Condition matches not URLs, but extended variables. The default relationship between conditons is "AND". That is to say, if one of conditon does not match, the match is exited. After one condition is matched, the next one is checked until it does not match, if all conditions match, the replacement will be performed.

Test conditions:

-F file exists;-d directory exists;-l is a symbolic link;-s file size is not 0

Rewrite rule parameters:

RewriteCond Command Format
Syntax: RewriteCond TestString CondPattern
1) TestString is a plain text string, but it can contain extensible Components
2) CondPattern is the condition pattern, that is, a regular expression applied to the current instance TestString, that is, TestString will be calculated and then matched with CondPattern.
3) In addition, you can append a special mark [flags] To CondPattern as the third parameter of the RewriteCond command. Flags is a list of the following tags separated by commas:
'Nocase | NC 'indicates that the test is case-insensitive, that is, TestString and CondPattern are case-insensitive.
'Ornext | or' it combines conditions of several rules in the OR mode, rather than implicit AND.

RewriteRule command
Syntax: RewriteRule Pattern Substitution
1) Pattern is a regular expression compatible with perl that acts on the current URL. The "current" here refers to the value of the URL when the rule takes effect.
2) Substitution is a string used to replace (or replace) the original URL and Pattern.
3) In addition, Substitution can append the special mark [flags] as the third parameter of the RewriteRule command. Flags is a list of the following tags separated by commas:
'Redirect | R [= code] '(Force redirect)
Substitution prefixed with http: // thishost [: thisport]/(make the new URL a URI) can forcibly execute an external redirection. If the code is not specified, an HTTP response code 302 (temporary movement) is generated ). To use other response codes in the range of-, you only need to specify this value here. In addition, you can also use one of the following symbol names: temp (default), permanent, seeother. the canonicalized URL can be fed back to the client. For example, rewrite "/~". "/U/", or add a slash to/u/user, and so on.

Note: When using this tag, make sure that the field to be replaced is a valid URL! Otherwise, it will point to an invalid location! Remember that this tag is only prefixed with http: // thishost [: thisport]/on the URL, and the rewrite operation will continue. In general, if you want to stop the rewrite operation and redirect immediately, you also need to use the 'l' flag.

'Forbidden | f' (force the URL to be a forbidden)
Force the current URL to be disabled. That is, an HTTP response code 403 (Forbidden) is immediately reported ). With this tag, you can link several RewriteConds to block some URLs with conditions.
'Gone | G' (force the URL to be an obsolete gone)
Force the current URL to be obsolete, that is, immediately feedback an HTTP response code 410 (obsolete ). Use this tag to indicate that the page has been deprecated and does not exist.
'Proxy | P' (Force proxy)
This flag forces the replacement component internally as a proxy request, and immediately transfers the process to the proxy module (that is, rewrite rule processing is interrupted immediately. You must ensure that the replacement string is valid (for example, a common URI starting with http: // hostname) and can be processed by the Apache proxy module. Using this tag, you can map some remote components to the local server namespace, thus enhancing the ProxyPass command function.
Note: To use this function, the proxy module must be compiled on the Apache server. If you are not sure, check whether mod_proxy.c exists in the output of "httpd-l. If yes, mod_rewrite can use this function. If not, you must enable mod_proxy and re-compile the "httpd" program.

'Last | l' (last rule last)
Stop the rewrite operation immediately and no other rewrite rules will be applied. It corresponds to the last command in Perl or the break command in C language. This mark can prevent the URL that has been rewritten from being overwritten by its subsequent rules. For example, you can use it to rewrite the root path URL ('/') to an existing URL, for example, '/e/www /'.
'Next | n' (re-execute next round)
Re-execute the rewrite operation (starting from the first rule again). At this time, the URL processed again is not the original URL, but the URL processed by the last rewrite rule. It corresponds to the next command in Perl or the continue command in C language. This flag allows you to re-start the rewrite operation, that is, immediately return to the loop header.
But be careful not to create an endless loop!
'Chain | C' (chained linked to the next rule)
This flag links the current rule with the next rule (which can be connected to its successor rule itself and can be so repeated. It produces the following effect: if a rule is matched, it will usually continue to process its successor rule, that is, this tag does not work; if the rule cannot be matched, the subsequent link rules are ignored. For example, when executing an external redirection, you may need to delete ". www" (". www" should not appear here) for a directory-level rule set ).
'Type | T = MIME-type' (mandatory MIME type)
The MIME type of the target file must be MIME-type. For example, it can be used to simulate the ScriptAlias command in mod_alias. The MIME type of all files in the ing directory must be "application/x-httpd-cgi ".
'Nosubreq | ns' (used only to process no internal sub-request for internal sub-requests)
When the current request is an internal subrequest, this flag forces the rewrite engine to skip this rewrite rule. For example, when mod_include tries to search for a possible default directory file (index. xxx), Apache will generate a request internally. It is not necessarily useful for subrequests. If the entire rule set works, it may even cause errors. Therefore, you can use this tag to exclude certain rules.

Follow the following principles as needed: if you use a URL prefix with CGI scripts to force them to be processed by CGI scripts, the error rate (or overhead) of sub-requests is high, in this case, you can use this tag.

'Nocase | NC '(case Insensitive)
It makes Pattern case insensitive, that is, when Pattern matches the current URL, there is no difference between 'a-Z' and 'a-Z.
'Qsappend | qsa' (append request string append)
This flag forces the rewrite engine to append a request string to an existing replacement string, instead of simply replacing it. If you need to add information to the request string by using the rewrite rule, you can use this flag.
'Noescape | nee' (no URI escaping is not escaped in the output)
This flag prevents mod_rewrite from applying regular URI escape rules to rewrite results. Generally, special characters (such as '%', '$', ';') are escaped as hexadecimal encoded values. This mark can prevent such escaping to allow symbols such as percent signs to appear in the output, for example:
RewriteRule/foo/(. *)/bar? Arg = P1 % 3d $1 [R, NE]

Can '/foo/zed' be redirected to a secure request'/bar? Arg = P1 = zed '.
'Passthrough | pt' (pass through to the next processor)
This flag forces the rewrite engine to set the uri field in the internal structure request_rec to the value of the filename field. It is only a small modification so that it can be used for the Alias, ScriptAlias, redirect and other command output for subsequent processing. For example, if you want to use the mod_rewrite rewrite engine to rewrite/abc as/def and then use mod_alias to convert/def to/ghi, you can do this:
RewriteRule ^/abc (. *)/def $1 [PT]
Alias/def/ghi

If the PT tag is omitted, although mod_rewrite works normally, that is, as a URI using the API to the file name translator, it can rewrite uri =/abc /... Is filename =/def /..., However, subsequent mod_alias attempts to translate the URI to the file name will become invalid.
Note: If you need to mix different modules that contain URIs to the file name translator, you must use this tag .. Mixed Use of mod_alias and mod_rewrite is a typical example.

For Apache hackers
If there is a hook from the URI to the file name in Apache API besides the hook from the URI to the file name, you do not need to mark it! However, if there is no such hook, this tag is the only solution. Apache Group discussed this issue and added such a hook in Apache 2.0.
'Skip | S = num' (skip the subsequent rule skip)
This flag forces the rewrite engine to skip the num rules next to the current matching rule. It can construct a pseudo if-then-else: the last rule is the then clause, And the skipped skip = N rules are the else clause. (it is different from the 'Chain | C' mark !)
'Env | E = VAR: VAL '(set the environment variable)
This flag sets the environment variable VAR value to VAL, which can contain extensible reverse referenced Regular Expressions $ N and % N. This tag can be used multiple times to set multiple variables. These variables can be indirectly referenced in many cases, but usually in XSSI (via <! -# Echo var = "VAR"->) or CGI (for example, $ ENV {'var'}), you can also run % {ENV in the pattern of the subsequent RewriteCond command: VAR} is referenced. You can use it to strip from the URL and remember some information.
'Cookie | CO = NAME: VAL: domain [: lifetime [: path] '(set cookie)
It sets a cookie on the client browser. The cookie NAME is NAME and its value is VAL. The domain field is the cookie domain, such as '.apache.org'. The optional life time is the number of minutes in the cookie life cycle, and the optional path is the cookie path.

In fact there is a http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_rewrite.html in the apache Manual

Apache official rewrite guide

For example, the following is wordpress rewrite. htaccess:

# BEGIN WordPress

RewriteEngine On
RewriteBase/

# Redirect all the URLs of learndiary.com to www.learndiary.com.
RewriteCond % {HTTP_HOST} ^ learndiary.com [NC]
RewriteRule ^ (. *) $ http://www.learndiary.com/#1 [L, R = 301]

# Remove the URL in the *. do format (required)
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_FILENAME }! -D
RewriteCond % {REQUEST_URI }!. +. Do
RewriteRule./index. php [L]

If you are on an nginx server, refer to zend framework settings under nginx

 

Add a virtual server setting in Linux. Set nginx. conf as follows:

Server {
Listen 80;
Server_name audit. local;
Root/app/audit/public;
Access_log/app/audit/logs/audit. access. log main;
Error_log/app/audit/logs/audit. error. log;
Location /{
Index. php;
# If file not found, redirect to Zend handling, we can remove the (if) here and go directly rewrite
If (! -F $ request_filename ){
Rewrite ^/(. +) $/index. php? $1 & last;
}
}
Location ~ * ^. +. (Js | ico | gif | jpg | jpeg | pdf | png | css) $ {
Access_log off;
Expires 7d;
}
Location ~ . *. Php? $ {
Fastcgi_pass 127.0.0.1: 36;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
}
Error_page 404 http://audit.local/error;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.