We know that there is a common function of. htaccess on apache, as long as the rewrite module is supported during apache compilation. We know that there is a common function of. htaccess on apache, as long as the rewrite module is supported during apache compilation. However, the method above nginx will be a little different. Many people on the Internet say that the. htaccess redirection rule is written into the nginx configuration file. This method is one of the official methods and is certainly feasible. However, this method has a defect: it is inconvenient. The next time you want to change a pseudo-static redirection rule, you have to change it in the nginx configuration file or the nginx virtual website configuration file, compared with apache, it is directly placed in the directory. the nginx Method for htaccess files is obviously primitive. But it doesn't matter. There is actually a solution. In the nginx configuration file
Include. htaccessFile to implement the same function. For example, to migrate. htaccess from www.blogguy.cn to nginx from apache, you can take the following steps:
Step 1: Modify the. htaccess FileBecause the conversion rules of apache rewrite are still different from those of nginx, the typical difference is that the nginx root directory needs to be written before the address for each line of redirection, each row of rules must end with a semicolon (;), and 301 or 404 redirects in different formats. The conversion from htaccess to nginx on apache is roughly as follows:
RewriteEngine OnRewriteBase/RewriteRule ^ show-([0-9] +)-([0-9] +) \. html $ index. php? Action = show & id = $1 & page = $2 RewriteRule ^ category-([0-9] +)-([0-9] + )\. html $ index. php? Action = index & cid = $1 & page = $2 RewriteRule ^ archives-([0-9] +)-([0-9] + )\. html $ index. php? Action = index & setdate = $1 & page = $2 RewriteRule ^ (archives | search | reg | login | index | links) \. html $ index. php? Action = $1 RewriteRule ^ (comments | tagslist | trackbacks | index)-([0-9] +) \. html $ index. php? Action = $1 & page = $2 rewriteCond % {http_host} ^ blogguy.cn [NC] rewriteRule ^ (. *) $ http://www.blogguy.cn/#1 [R = 301, L] ErrorDocument 404 http://www.blogguy.cn/
Rules for converting to nginx
Rewrite ^/show-([0-9] +)-([0-9] +) \. html $/index. php? Action = show & id = $1 & page = $2; rewrite ^/category-([0-9] +)-([0-9] + )\. html $/index. php? Action = index & cid = $1 & page = $2; rewrite ^/archives-([0-9] +)-([0-9] + )\. html $/index. php? Action = index & setdate = $1 & page = $2; rewrite ^/(archives | search | reg | login | index | links) \. html $/index. php? Action = $1; rewrite ^/(comments | tagslist | trackbacks | index)-([0-9] +) \. html $/index. php? Action = $1 & page = $2; if ($ host! = 'Www .blogguy.cn ') {rewrite ^/(. *) $ http://www.blogguy.cn/#1 permanent;} error_page 404 http://www.blogguy.cn /;
Step 2: Modify the nginx configuration file and add the include. htaccess file.
Vi/etc/nginx/sites-available/www.blogguy.cn
Add one line: include/var/www. blogguycn/. htaccess and modify it to your corresponding address.
Step 4: Test and restart
/Etc/init. d/nginx-configtest
If there is no problem, restart:/etc/init. d/nginx restart to check the effect. Is it OK?