. Htaccess configuration details

Source: Internet
Author: User

. Htaccess configuration details

1. Enable mod_rewrite
Set # LoadModule rewrite_module modules/mod_rewrite.so
This line # Remove

2. Modify httpd. conf
1) configure
Change AllowOverride None to AllowOverride All.

2) Modify Options attributes
It is recommended to change to Indexes FollowSymLinks.

FollowSymLinks
The server allows a symbolic connection in this directory.
Indexes
If a URL mapped to a directory is requested and there is no DirectoryIndex (for example, index.html) in the directory, the server returns a formatted directory list generated by mod_autoindex.
Note: Because the default value of Options after apache2.2 is None, a 403 error occurs when. htaccess is enabled. This problem has been plagued for a long time because it was not noticed at first. All programs that cannot test the re_write function locally for a long time.

When I checked apache error. log last night
[Sat Aug 09 23:06:19 2008] [error] [client 127.0.0.1] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: G: /hekey/www/111/post/0, referer: http: // localhost/111/admin. php tutorial? Go = misc_urlrewrite
The Options issue is known only when the error occurs.


--------------------------------------------------------------------------------

I have found many articles on how to configure apache mod_rewrite on the Internet. I find this article useful. For more information, see.

1. view the environment configuration through the phpinfo () function provided by php, and find "Loaded Modules" Through Ctrl + F, which lists all Modules enabled by apache2handler, if "mod_rewrite" is included, it is supported and you do not need to set it again.
If "mod_rewrite" is not enabled, open httpd in the directory of your apache installation directory "/apache/conf. in the conf file, use Ctrl + F to find "LoadModule rewrite_module" and delete. If not found, add "LoadModule rewrite_module modules/mod_rewrite.so" to the last row in the "LoadModule" area, and then restart the apache server.

2. Enable the apache server to support ". htaccess"
How can I enable my local APACHE server to support ". htaccess? In fact, you only need to modify the httpd. conf settings of apache to enable APACHE to support. htaccess. Open the httpd. conf file (where? In the CONF directory of the APACHE directory.
<Directory/>
Options FollowSymLinks
AllowOverride None
</Directory>
Change
<Directory/>
Options FollowSymLinks
AllowOverride All
</Directory>
You can.

3. Create a ". htaccess" file.
If it is on a windows platform, I don't know how to create it at the beginning ". "htaccess" file, because this file does not actually have a file name and only has a file extension. You cannot create this file in a common way. Don't worry. I will tell you three methods right away:
You can create an htaccess.txt text file first (of course, you can name this text file as needed) in the three types of scripts. There are three ways to rename this file: (1) Open it in notepad, click File-save, in the file name window, enter ". htaccess ", note that the entire Green part, that is, contains English quotation marks, and then click Save. (2) Enter the authorization command window, switch to the folder where the htaccess.txt file is just created, Enter the command: rename htaccess.txt. htaccess, and then click Enter. (32.16connect to the folder where htaccess.txt is located through ftpand rename it through ftp software.
So how many ". htaccess" files need to be created, and what content should be entered in them? The basic principle is that the root directory must have one. All requests for redirection (URl rewriting) are forwarded to the index. php (to the front-end Controller );". the/application folder requires one to deny all direct access to the folder content (such as http: // localhost/application/models/User. php). This is because all access requests must be allocated by the front-end controller, followed by security. ". /library htaccess "Same as before ;". /public "folder must be selected, because all the files in this folder are directly accessed by the front-end, so you need to cancel URl rewriting. The following are the content of Four "./htaccess" files:
./. Htaccess
RewriteEngine on
RewriteRule! /. (Js | ico | gif | jpg | png | css tutorial) $ index. php
./Application/. htaccess
Deny from all
./Library/. htaccess
Deny from all
./Public
RewriteEngine off
In fact, the url rewriting function of apache can also be set directly in the httpd. conf file, which is not detailed here. There are also many online tutorials.


--------------------------------------------------------------------------------

Rewrite flag

R [= code] (force redirect) force external redirection
Force the replacement string to add http: // thishost [: thisport]/prefix to the external URL. If the code is not specified, the default 302 HTTP status code will be used.
F (force URL to be forbidden) disables the URL and returns a 403HTTP status code.
G (force URL to be gone) forces the URL to be GONE and returns the response HTTP status code.
P (force proxy) enforces proxy forwarding.
L (last rule) indicates that the current rule is the last rule. After the analysis is stopped, the rule is overwritten.
N (next round) re-runs the rewrite process from the first rule.
C (chained with next rule) is associated with the next rule
If the rule matches, it is processed normally and the flag is invalid. If the rule does not match, all associated rules are skipped.
T = MIME-type (force MIME type) force MIME type
NS (used only if no internal sub-request) is only used for internal subrequests
NC (no case) is case insensitive
QSA (query string append) append request string
NE (no URI escaping of output) does not escape special characters from the output
Example: RewriteRule/foo/(. *)/bar? Arg = P1/% 3d $1 [R, NE] will be able to correctly convert/foo/zoo to/bar? Arg = P1 = zed
PT (pass through to next handler) passed to the next Processing
For example:
RewriteRule ^/abc (. *)/def $1 [PT] # will be handed over to/def rules for processing
Alias/def/ghi
S = num (skip next rule (s) skips num rules
E = VAR: VAL (set environment variable) set environment Variables

Server variables during rewrite:
HTTP headers: HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_HOST, HTTP_ACCEPT
Connection & request: REMOTE_ADDR, QUERY_STRING
Server internals: DOCUMENT_ROOT, SERVER_PORT, SERVER_PROTOCOL
System stuff: TIME_YEAR, TIME_MON, TIME_DAY

Description of the Rewrite rule expression:
. Match any single character
[Chars] matched string: chars
[^ Chars] unmatched string: chars
Text1 | string that can be selected by text2: text1 or text2
? Matches 0 to 1 Characters
* Matches 0 to multiple characters
+ Match 1 to multiple characters
^ String start flag
$ String end flag
/N Escape Character Mark

Reverse reference $ N is used to call the matching variable in RewriteRule (0 <= N <= 9)
Reverse reference % N is used to call the last matched variable in RewriteCond (1 <= N <= 9)

RewriteCond identifier
'Nocase | NC '(no case) Ignore size
'Ornext | or' (OR next condition) logic or, multiple RewriteCond conditions can be matched simultaneously

The applicable identifier of RewriteRule http //: www.zhutiai.com
'Redirect | R [= code] '(force redirect) force rewrite to an external redirection starting with http (note URL changes) such as: [R = 301, L]
'Forbidden | f' (force URL to be forbidden) is rewritten to prohibit access.
'Proxy | P' (force proxy) is rewritten to the http path accessed through the proxy
'Last | l' (last rule) indicates the last rewrite rule flag. If it matches, it will not be executed.
'Next | n' (next round) loops through the same rule until it cannot match
'Chain | C' (chained with next rule) If this rule is matched, the following chain-marked rule is continued.
'Type | T = MIME-type' (force MIME type) specifies the MIME type
'Nosubreq | ns' (used only if no internal sub-request)
'Nocase | NC '(no case) Ignore size
'Qsappend | qsa' (query string append) append the query string.
'Noescape | ne' (no URI escaping of output) disables characters in the URL from being automatically converted to % [0-9] +.
'Passthrough through to next handler 'applies the rewriting result to mod_alias
'Skip | S = num' (skip next rule (s) skips the following rules
'Env | E = VAR: VAL '(set environment variable) to add environment Variables

Actual Operation

Example:
RewriteEngine on
RewriteCond % {HTTP_USER_AGENT} ^ MSIE [NC, OR]
RewriteCond % {HTTP_USER_AGENT} ^ Opera [NC]
RewriteRule ^. *-[F, L] Here "-" indicates no replacement. Visitors with IE and Opera browsers will be banned from accessing the site.

Example:
RewriteEngine On
RewriteBase/test
RewriteCond % {REQUEST_FILENAME}. php-f
RewriteRule ([^/] +) $/test/$ 1.php
# For example:/test/admin =>/test/admin. php
RewriteRule ([^/] +)/. html $/test/$ 1.php [L]
# For example:/test/admin.html =>/test/admin. php

Restrict directories to display images only
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond % {REQUEST_FILENAME }! ^. */. (Gif | jpg | jpeg | png | swf) $
RewriteRule. * $-[F, L]
</IfModule>

 

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.