1. Disguise dynamic addresses as static addresses
This is the most common and simple. The so-called dynamic address is the URL with parameters.
For example, the real access address is http://www.test.com/a.php? B = test but want to achieve static address through rewrite, convenient user access want to change to http://www.test.com/test.html
Apache:
RewriteEngine On
RewriteRule/test.html/a. php? B = test [L]
Nginx:
Rewrite "/test.html"/a. php? B = test last
In fact, nginx rewrite is simpler than apache, with at least less data to be written. Note 1. Nginx camouflage address requires double quotation marks.
2. Real addresses do not need to be escaped
3. Change [L] to last.
4. Apache can be put in. htaccess, but nginx cannot.
2. Complex address camouflage
In fact, this is the same as the rewrite rule. It only uses complex regular expressions and common variables to make rewrite rules adapt to all of them.
3. Parameter-based address disguise
Rewrite has a feature. Whether it is apache or nginx, it means that parameters cannot be passed on the disguised address of the rewrite rule using the above method? As a simultaneous expression. Of course, there must be a solution. Our website is facing this problem.
For example: want users to enter http://www.test.com/a.jsp? Model = 3500c while accessing http://www.test.com/ B .php? Id = 10
There is no rule to transfer parameters.
Apache:
RewriteEngine On
RewriteCond % {QUERY_STRING} ^ Model = 3500c $
RewriteRule ^ a. jsp $ B. php? Id = 10 [L]
Nginx:
If ($ query_string = "Model = 3500c "){
Rewrite ^/B. php? Id = 10 last;
}
For example, only the rewrite variables and if statements can be used to pass parameters. nginx is relatively simple. if you want to implement more functions, view the nginx rewrite parameters below, it can be implemented through combination.
Wordpress redirection rules:
If (! -E $ request_filename ){
Rewrite ^/(index | atom | rsd). xml $ http://feed.shunz.net last;
Rewrite ^ ([_ 0-9a-zA-Z-] + )? (/Wp-. *) $2 last;
Rewrite ^ ([_ 0-9a-zA-Z-] + )? (/. *. Php) $2 last;
Rewrite ^/index. php last;
}
Discuz! Redirection rules:
If (! -F $ request_filename ){
Rewrite ^/archiver/(fid | tid0000-0000w-00000000.html) $/archiver/index. php? $1 last;
Rewrite ^/forum-(%0-9%%%%-(%0-9%%%%%%.html $/forumdisplay. php? Fid = $1 & page = $2 last;
Rewrite ^/thread-(0-9000000000000-(0-9000000000000-(0-90000000000000000.html $/viewthread. php? Tid = $1 & extra = page % 3D $3 & page = $2 last;
Rewrite ^/space-(username | uid316-(.20.0000.html $/space. php? $1 = $2 last;
Rewrite ^/tag-(.20.20..html $/tag. php? Name = $1 last;
}
Example of combining QeePHP
If (! -D $ request_filename ){
Rewrite ^/([a-z-A-Z] +)/([a-z-A-Z] + )/? (. *) $/Index. php? Namespace = user & amp; controller = $1 & amp; action = $2 & amp; $3 last;
Rewrite ^/([a-z-A-Z] + )/? $/Index. php? Namespace = user & amp; controller = $1 last;
Break;
Convert multiple directories to parameters
Abc.domian.com/sort/2 => abc.domian.com/index.php? Act = sort & name = abc & id = 2
If ($ host ~ * (. *)/. Domain/. com ){
Set $ sub_name $1;
Rewrite ^/sort // (/d + )//? $/Index. php? Act = sort & cid = $ sub_name & id = $1 last;
}
Directory swap
/123456/xxxx->/xxxx? Id = 123456
Rewrite ^/(/d +)/(. +)/$2? Id = $1 last;
For example, set nginx to redirect to the/nginx-ie directory when you use ie:
If ($ http_user_agent ~ MSIE ){
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
Automatically add "/" to the Directory
If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}
Disable htaccess
Location ~ //. Ht {
Deny all;
}
Prohibit multiple directories
Location ~ ^/(Cron | templates )/{
Deny all;
Break;
}
Prohibit files starting with/data
You can disable/data/multi-level Directory. Log.txt and other requests;
Location ~ ^/Data {
Deny all;
}
Disable a single directory
Unable to stop. Log.txt requests
Location/searchword/cron /{
Deny all;
}
Prohibit a single file
Location ~ /Data/SQL/data. SQL {
Deny all;
}
Set the expiration time for favicon.icoand robots.txt;
In this example, if favicon.icois set to 404 days, and Robots.txt is set to 7 days, error logs are not recorded.
Location ~ (Favicon. ico ){
Log_not_found off;
Expires 99d;
Break;
}
Location ~ (Robots.txt ){
Log_not_found off;
Expires 7d;
Break;
}
Set the expiration time of a file. The value is 600 seconds and no access logs are recorded.
Location ^ ~ /Html/scripts/loadhead_1.js {
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 600;
Break;
}
File anti-leeching and set expiration time
Here, return 412 is a custom http status code. The default value is 403, which helps you find the correct request for leeching.
"Rewrite ^/yun_qi_img/leech.gif;" shows an anti-Leech image.
"Access_log off;" does not record access logs, reducing pressure
"Expires 3d" browser cache for all files for 3 days
Location ~ * ^. +/. (Jpg | jpeg | gif | png | swf | rar | zip | css | js) $ {
Valid_referers none blocked * .c1gstudio.com * .c1gstudio.net localhost 208.97.167.194;
If ($ invalid_referer ){
Rewrite ^/yun_qi_img/leech.gif;
Return 412;
Break;
}
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 3d;
Break;
}
Only use a fixed ip address to access the website and add a password.
Root/opt/htdocs/www;
Allow 208.97.167.194;
Allow 222.33.1.2;
Allow 231.152.49.4;
Deny all;
Auth_basic "C1G_ADMIN ";
Auth_basic_user_file htpasswd;
Convert files under multi-level directories into one file to Improve seo performance
/Job-123-456-789.html to/job/123/456/789.html
Rewrite ^/job-([0-9] +)-([0-9] +)-([0-9] + )/. html $/job/$1/$2/jobshow_#3.html last;
Point a folder in the root directory to a level 2 Directory
For example,/shanghaijob/points to/area/shanghai/
If you change last to permanent, the address bar of the browser is/location/shanghai/
Rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
The problem in the above example is that the access/shanghai will not match
Rewrite ^/([0-9a-z] +) job $/area/$1/last;
Rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
In this way,/shanghai can also be accessed, but the relative link on the page cannot be used,
For example, if the actual address of./list_1.html is/area/shanghia/list_1.html, it will become/list_1.html and cannot be accessed.
I cannot add automatic jump.
(-D $ request_filename) it has a condition that it must be a real directory, and my rewrite is not, so it has no effect
If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}
After knowing the reason, let me jump to it manually.
Rewrite ^/([0-9a-z] +) job $/$ 1job/permanent;
Rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
Redirection when the file and directory do not exist:
If (! -E $ request_filename ){
Proxy_pass http: // 127.0.0.1;
}
Domain jump
Server
{
Listen 80;
Server_name jump.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/www;
Rewrite ^/http://www.c1gstudio.com /;
Access_log off;
}
Multi-domain redirection
Server_name www.c1gstudio.com www.c1gstudio.net;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs;
If ($ host ~ "C1gstudio/. net "){
Rewrite ^ (. *) http://www.c1gstudio.com $1 permanent;
}
Third-level domain jump
If ($ http_host ~ * "^ (. *)/. I/. c1gstudio/. com $ "){
Rewrite ^ (. *) http://top.yingjiesheng.com $1;
Break;
}
Domain name mirror
Server
{
Listen 80;
Server_name pai.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/www;
Rewrite ^/(. *) http://www.c1gstudio.com/#1 last;
Access_log off;
}
A subdirectory for mirroring
Location ^ ~ /Zhaopinhui {
Rewrite ^. + http://zph.c1gstudio.com/last;
Break;
}
Nginx rewrtie parameters
Regular Expression Matching, where:
*~ Case-sensitive matching
*~ * Case-insensitive match
*!~ And !~ * Case-insensitive and case-insensitive
File and directory match, where:
*-F and! -F is used to determine whether a file exists.
*-D and! -D is used to determine whether a directory exists.
*-E and! -E is used to determine whether a file or directory exists.
*-X and! -X is used to determine whether a file is executable.
Flag labels include:
* Last is equivalent to the [L] Mark in Apache, indicating that rewrite is completed.
* The break terminates the match and does not match the subsequent rules.
* Redirect returns 302 temporary redirection
* Permanent returns 301 permanent redirection