Apache URL Rewrite converted to NGINX URL Rewrite tool
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/
First of all, Apache’s Rewrite rules are not very different, but Nginx’s Rewrite rules are much simpler and more flexible than Apache’s
Nginx can use if for conditional matching, and the syntax rules are similar to C
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1
break;
}
For official documents, please click here
Flags of Rewrite
Flags can be any of the following:
* last – completes processing of rewrite directives, after which
searches for corresponding URI and location
* break – completes processing of rewrite directives
*redirect – returns temporary redirect with code 302; it is used if
the substituting line begins with http://
* permanent – returns permanent redirect with code 301
last-
After completing the rewrite instruction, search for the corresponding URI and location. It is equivalent to the [L] mark in Apache, which means that the rewrite is completed and no longer matches the following rules.
break – Abort Rewirte, do not continue matching.
redirect – Returns HTTP status 302 for a Temporary Redirect.
permanent-
Returns HTTP status 301 for Permanent Redirect.
Redirection rules for ZEND Framework:
Case 1:
Redirect all to /index.php
rewrite ^/(.*) /index.php?$1&;
Case 2:
Redirect to index.php if file or directory does not exist
if (!-e $request_filename) {
rewrite ^/(.*) /index.php?$1&;
}
WordPress redirection rules:
Case 1:
http://www.wemvc.com/12 redirects to
http://www.wemvc.com/index.php?p=12
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php?p=$1 last;
}
Case 2:
Similar to zendframework configuration
if (!-e $request_filename) {
rewrite ^/(.*)
/index.php?$1&;
}
The following is Discuz’s complete Rewrite for Nginx rules
if (!-f $request_filename) {//If the file does not exist, it means that it is a simulated static file, and the URL is rewritten
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$
/archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$
/forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$
/viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2
last;rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
}
Files and directories match, where:
-f and !-f are used to determine whether a file exists
-d and !-d are used to determine whether a directory exists
-e and !-e are used to determine whether a file or directory exists
-x and !-x are used to determine whether the file is executable
Explanation of all symbols in regular expressions
~ for case sensitive matching
~* for insensitive case matching
!~ and !~* are case-sensitive mismatch and case-insensitive mismatch respectively
(pattern) matches pattern and retrieves this match. The obtained matches can be obtained from the generated Matches collection, in Vbscript
Use the SubMatches collection in , and the $0…$9 properties in JScript. To match parenthesis characters, use ‘\(‘ or
‘\)’.
^ matches the beginning of the input string.
$ matches the end of the input string.
###### bbs.uenu.com #####################
server {
listen
80;
server_name bbs.uenu.com;
location / {
root
E:/home/bbs/htdocs;
index index.html
index.htm index.php default.php;
rewrite
^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1
last;
rewrite
^/forum-([0-9]+)-([0-9]+)\.html$
/forumdisplay.php?fid=$1&page=$2 last;
rewrite
^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$
/viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite
^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite
^/tag-(.+)\.html$ /tag.php?name=$1 last;
}
error_page 500 502 503 504
/50x.html;
location = /50x.html {
root
html;
}
location ~ .*\.php?$ {
root
E:/home/bbs/htdocs;
“ fastcgi_pass`
127.0.0.1:9000;
fastcgi_index
index.php;
fastcgi_param
SCRIPT_FILENAME
E:/home/bbs/htdocs$fastcgi_script_name;
include
fastcgi_params;
}
}
###############################nginx
Detailed explanation of rewrite parameters##############################
There are some available global variables, which can be used for conditional judgment (to be completed)
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_COOKIE
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
Example:
abc.domian.com/sort/2 =>
abc.domian.com/index.php?act=sort&name=abc&id=2
hostname case-insensitive matches (.*).domain.com
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$
/index.php?act=sort&cid=$sub_name&id=$1 last;
}
It’s a pity that else cannot be used for testing. In addition, I don’t know how to test some of the redirected printouts.
You can only know whether it is ok or not by checking the effect after re-validating the configuration. Alas, the rookie presents, the prawn gives pointers
Continuation:
Example with QeePHP
The requested directory does not exist
1. if (!-d $request_filename) {
2. rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$
3.
/index.php?namespace=user&cOntroller=$1&action=$2&$3
4. last;
5. rewrite ^/([a-z-A-Z]+)/?$
/index.php?namespace=user&cOntroller=$1
6. last;
7. break;
Convert multiple directories into parameters
abc.domian.com/sort/2 =>
abc.domian.com/index.php?act=sort&name=abc&id=2
1. if ($host ~* (.*)\.domain\.com) {
2. set $sub_name $1;
3. rewrite ^/sort\/(\d+)\/?$
/index.php?act=sort&cid=$sub_name&id=$1
4. last;
5. }
directory swap
/123456/xxxx -> /xxxx?id=123456
1. rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
For example, set nginx to redirect to the /nginx-ie directory when the user uses ie:
1. if ($http_user_agent ~ MSIE) {
2. rewrite ^(.*)$ /nginx-ie/$1 break;
3. }
The directory automatically adds “/”
1. if (-d $request_filename){
2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; 301 permanent redirect
3. }
Disable htaccess
1. location ~/\.ht {
2. deny all;
3. }
disallow multiple directories
1. location ~ ^/(cron|templates)/ {
2. deny all;
3. break;
4. }
Disallow files starting with /data
Requests such as .log.txt in the multi-level directory under /data/ can be prohibited;
1. location ~ ^/data {
2. deny all;
3. }
Forbid single directory
Can’t disable .log.txt can request
1. location /searchword/cron/ {
2. deny all;
3. }
Suppress individual files
1. location ~ /data/sql/data.sql {
2. deny all;
3. }
Set expiration time for favicon.ico and robots.txt;
Here favicon.ico is 99 days, robots.txt is 7 days and does not record 404 error logs
1. location ~(favicon.ico) {
2. log_not_found off;
3. expires 99d;
4. break;
5. }
6.
7. location ~(robots.txt) {
8. log_not_found off;
9. expires 7d;
10. break;
11. }
Set the expiration time of a file; here it is 600 seconds, no access log is recorded
1. location ^~ /html/scripts/loadhead_1.js {
2. access_log off;
3. root /opt/lampp/htdocs/web;
4. expires 600;
5. break;
6. }
File anti-hotlinking and set expiration time [Refer to: Nginx anti-hotlinking configuration]
The return 412 here is a custom http status code, the default is 403, which is convenient for finding the correct hotlink request
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;” displays an anti-leech picture
“access_log off;” does not record access logs, reducing pressure
“expires 3d” 3 days of browser cache for all files
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 ^/
http://leech.c1gstudio.com/leech.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
Only allow fixed ip access to the website, and add a password
1. root /opt/htdocs/www;
2. allow 208.97.167.194;
3. allow 222.33.1.2;
4. allow 231.152.49.4;
5. deny all;
6. auth_basic “C1G_ADMIN”;
7. auth_basic_user_file htpasswd;
Convert files in multi-level directories into one file to enhance seo effect
/job-123-456-789.html
point to /job/123/456/789.html
1. rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$
/job/$1/$2/jobshow_$3.html last;
Point a folder under the root directory to the level 2 directory
For example, /shanghaijob/ points to /area/shanghai/
If you change last to permanent, the address bar of the browser will display /location/shanghai/
1. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
The problem with the above example is that when accessing /shanghai, it will not match
1. rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
This way/shanghai
It can also be accessed, but the relative links in the page cannot be used,
For example, the real address of ./list_1.html is /area/shanhia/list_1.html and it will become /list_1.html, making it inaccessible.
Then it won’t work if I add automatic jump
(-d $request_filename) It has a condition that it must be a real directory, but my rewrite is not, so it has no effect
1. if (-d $request_filename){
2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3. }
It will be easy to handle once you know the reason, let me jump manually
1. rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
Redirect when files and directories do not exist:
1. if (!-e $request_filename) {
2. proxy_pass http://127.0.0.1;
3. }
Domain name jump
1. server
2. {
3. listen 80;
4. server_name jump.c1gstudio.com;
5. index index.html index.htm index.php;
6. root /opt/lampp/htdocs/www;
7. rewrite ^/ http://www.c1gstudio.com/;
8. access_log off;
9. }
Third-level domain name jump
1. if ($http_host ~* “^(.*)\.i\.c1gstudio\.com$”) {
2. rewrite ^(.*) http://top.yingjiesheng.com$1;
3. break;
4. }
Domain name mirroring
1. server
2. {
3. listen 80;
4. server_name mirror.c1gstudio.com;
5. index index.html index.htm index.php;
6. root /opt/lampp/htdocs/www;
7. rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
8. access_log off;
9. }
A subdirectory as a mirror
1. location ^~ /zhaopinhui {
2. rewrite ^.+ http://zph.c1gstudio.com/ last;
3. break;
4. }
G_ADMIN”;
7. auth_basic_user_file htpasswd;
Convert files in multi-level directories into one file to enhance seo effect
/job-123-456-789.html
point to /job/123/456/789.html
1. rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$
/job/$1/$2/jobshow_$3.html last;
Point a folder under the root directory to the level 2 directory
For example, /shanghaijob/ points to /area/shanghai/
If you change last to permanent, the address bar of the browser will display /location/shanghai/
1. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
The problem with the above example is that when accessing /shanghai, it will not match
1. rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
This way/shanghai
It can also be accessed, but the relative links in the page cannot be used,
For example, the real address of ./list_1.html is /area/shanhia/list_1.html and it will become /list_1.html, making it inaccessible.
Then it won’t work if I add automatic jump
(-d $request_filename) It has a condition that it must be a real directory, but my rewrite is not, so it has no effect
1. if (-d $request_filename){
2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3. }
It will be easy to handle once you know the reason, let me jump manually
1. rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
Redirect when files and directories do not exist:
1. if (!-e $request_filename) {
2. proxy_pass http://127.0.0.1;
3. }
Domain name jump
1. server
2. {
3. listen 80;
4. server_name jump.c1gstudio.com;
5. index index.html index.htm index.php;
6. root /opt/lampp/htdocs/www;
7. rewrite ^/ http://www.c1gstudio.com/;
8. access_log off;
9. }
Third-level domain name jump
1. if ($http_host ~* “^(.*)\.i\.c1gstudio\.com$”) {
2. rewrite ^(.*) http://top.yingjiesheng.com$1;
3. break;
4. }
Domain name mirroring
1. server
2. {
3. listen 80;
4. server_name mirror.c1gstudio.com;
5. index index.html index.htm index.php;
6. root /opt/lampp/htdocs/www;
7. rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
8. access_log off;
9. }
A subdirectory as a mirror
1. location ^~ /zhaopinhui {
2. rewrite ^.+ http://zph.c1gstudio.com/ last;
3. break;
4. }