Suppose httpd. the DocumentRoot in conf is DocumentRoot "D:/Apache3W". Use the <Directory> command to set url rewriting in httpd. conf writes the following content: <Directory "D:/Apache3W"> RewriteEngine on RewriteRule ^ ([^ \.] +) $ 1.php Options Indexes FollowSymLinks AllowOverride All Order allow, deny Allow from all </Directory> <Directory "D:/Apache3W/phplearn"> # if nothing is written here, url rewriting settings will also be applied, because D:/Apache3W/phplearn is a subdirectory of D:/Apache3W, so the setting of this directory instruction will inherit the instruction setting of its parent directory, if this directory does not want to rewrite the url, you must explicitly specify: RewriteEngine off # If RewriteEngine on is written but RewriteRule is specified here, this directory cannot rewrite the url, because RewriteEngine on overwrites the url rewriting settings of the parent Directory, the RewriteRule of the parent Directory becomes invalid </Directory>. set url rewriting in the htaccess file, httpd. specify the following content in conf: <Directory "D:/Apache3W"> Options Indexes FollowSymLinks AllowOverride All Order allow, deny Allow from all </Directory> <Directory "D: /Apache3W/phplearn "> </Directory> created in D:/Apache3W. the content of the htaccess file is as follows: RewriteEngine onRewriteRule ^ ([^ \.] +) $1. php implements url rewriting on D:/Apache3W and D:/Apache3W/phplearn. the htaccess file contains the following: Only D:/Apache3W implements url rewriting and change D:/Apache3W/phplearn </Directory> command: <Directory "D: /Apache3W/phplearn "> AllowOverride None </Directory> found that D:/Apache3W/phplearn implements url rewriting again, because D:/Apache3W/phplearn is disabled. in the htaccess file, D:/Apache3W/phplearn inherits from the directory on the previous layer. htaccess File
Scattered:
1. Set all access to the test Directory to <Directory ~ "/Test/"> Order deny, allow Allow from all </Directory>
This cannot be written as <Directory ~ "/Test $"> or <Directory ~ "/Test [\ s \ S] * $">. Otherwise, the folder cannot be found and the permission does not work, all <Directory> matches with regular expressions. If regular expressions end with $, no matching is found.
2. the url rewriting Instruction Set in <Directory>'s regular expression match and wildcard match is invalid, and the implemented url rewriting will also be damaged, for example:
<Directory/usr/dev/apache3w/*> RewriteEngine On RewriteRule ^ (\ w + )\.? (? : \ W + )? $ 1.php</Directory>
Or <Directory ~ "/Test/"> RewriteEngine On RewriteRule ^ (\ w + )\.? (? : \ W + )? $ 1.php</Directory>
You cannot rewrite the URLs of files in the/usr/dev/apache3w/test directory. to rewrite the URLs, you must use the complete path, as shown in figure
<Directory/usr/dev/apache3w/test> RewriteEngine On RewriteRule ^ (\ w + )\.? (? : \ W + )? $ 1.php</Directory>
In addition, setting url rewriting in <Directory> Regular Expression matching and wildcard matching will not only be invalid, but also overwrite valid url rewriting. For example:
(1) set url rewriting in Directory wildcard matching, if the order is after the valid url rewriting settings
<Directory/usr/dev/apache3w/test> RewriteEngine On RewriteRule ^ (\ w + )\.? (? : \ W + )? $ 1.php</Directory> <Directory/usr/dev/apache3w/*> RewriteEngine On RewriteRule ^ (\ w + )\.? (? : \ W + )? $ 1.php
</Directory>
(2) Set url rewriting in Directory Regular Expression matching (note that regular expression matching in Directory can also be done without quotation marks)
<Directory ~ /Test/> RewriteEngine On RewriteRule ^ (\ w + )\.? (? : \ W + )? $ 1.php</Directory> <Directory/usr/dev/apache3w/test> RewriteEngine On RewriteRule ^ (\ w + )\.? (? : \ W + )? $ 1.php</Directory>
3. Files command
The Files command provides file name-Based Access Control. The priority is<Directory>
Segment and.htaccess
After the file is processed<Location>
Before Section
For example
<Files ~ \. Php $>
Deny from all
</Files>
All PHP files are denied access.
<Directory "/usr/dev/apache3w/test2/test"> <Files index. php> Deny from all </Files>
</Directory>
Indicates that access to/usr/dev/apache3w/test2/test/index. php is denied, but other php files in the same directory can be accessed.
4. Configure multiple websites
(1) Listen 80 for multiple websites on different ports
Listen 81
<VirtualHost *: 80>
ServerAdmin lx@qq.com
DocumentRoot "D:/Apache3W"
ErrorLog logs/81-error_log.log
TransferLog logs/81-access_log.log
</VirtualHost>
<VirtualHost *: 81>
ServerAdmin lx@qq.com
DocumentRoot "D:/Apache3W_81"
ErrorLog logs/81-error_log.log
TransferLog logs/81-access_log.log
</VirtualHost>
(2) multiple websites on the same port
Listen 80
NameVirtualHost *: 80
<VirtualHost *: 80>
ServerAdmin lx@qq.com
ServerName a.lexun.com
DocumentRoot "D:/Apache3W"
ErrorLog logs/a.test.com-error_log.log
TransferLog logs/a.test.com-access_log.log
</VirtualHost>
<VirtualHost *: 80>
ServerAdmin lx@qq.com
ServerName B .lexun.com
DocumentRoot "D:/Apache3W_81"
ErrorLog logs/B .test.com-error_log.log
TransferLog logs/B .test.com-access_log.log
</VirtualHost>
5. About AddHandler and AddType (one is the resource used by the server to process requests, and the other is the mime type of the client browser)
Assume that both AddHandler and AddType are set in server config and are not covered by the quilt level (1)
AddHandler application/x-httpd-php. php
AddType image/jpg. php
Result:
The value of AddHandler has a higher priority, so accessing localhost/index. php will parse php normally.
(2)
AddHandler image/jpg. php
Result:
Response Headers have the following headers: Content-Type: text/plain
Because the server cannot find the. php processor, the php file is output as the original txt script.
(3)
AddType image/jpg. php
Result:
Response Header has the following Header: Content-Type: image/jpg
The server uses php as the original txt text output, but because. php is not a jpg image type, the following prompt is displayed when you use Firefox to browse localhost/index. php: Image
"Http: // localhost/index. php" cannot be displayed due to its own error.
(4)
AddHandler image/jpg. php
AddType image/jpg. php
Result:
Response Header has the following Header: Content-Type: image/jpg
Like (3), this is because AddHandler has a higher priority, but cannot find the image/jpg processor and uses a text processor, so it will be followed by AddType image/jpg. overwrite php
Tip: After changing httpd. conf to restart the server, it is recommended that the browser clear the cache, close the tag, and then open the page.
Run the PHP configuration in the module, FastCgi, and Cgi modes respectively (test environment: Win7 + Apache2.2 + PHP5.3.5)
# Use apache module
# LoadModule php5_module "D: \ dev \ php_5.3.5 \ php5apache2_2.dll"
# AddHandler application/x-httpd-php. php
# PHPIniDir "D: \ dev \ php_5.3.5"
# Use fastcgi
# LoadModule fcgid_module modules/mod_fcgid.so
# FcgidInitialEnv PHPRC "D: \ dev \ php_5.3.5"
# AddHandler fcgid-script. php
# FcgidWrapper "D: \ dev \ php_5.3.5 \ php-cgi.exe". php
# Use cgi Note: use/instead of \ here, or the following error will occur (assuming that the access
Http: // localhost/index. php ):
The request URL/php/php-cgi.exe/index. php was not found on this server
ScriptAlias/php/"D:/dev/php_5.3.5 /"
AddType application/x-httpd-php. php
Action application/x-httpd-php "/php/php-cgi.exe"
Example of Using cgi Mode with ScriptAlias
1. Configure the cgi Mode in httpd. conf as described above
2. Create test. php In D:/dev/php_5.3.5/. The content is as follows:
#! D:/dev/php_5.3.5/php-cgi.exe
<? Php
Echo 'Hello world ';
?>
3. Change php. ini: cgi. force_redirect = 0
4. Restart Apache
Then access http: // localhost/php/test. php and the page runs normally. If you remove #! D:/dev/php_5.3.5/the declaration of the php-cgi.exe or cgi. force_redirect in php. ini is not equal to 0, an error is reported on the page.