How to Support. htaccess pseudo-static redirection on nginx

Source: Internet
Author: User
We know that apache has a common function. as long as the rewrite module is supported during apache compilation, we know that there is a common function in apache. to turn to htaccess, you only need to specify that 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. In fact, there is a way to implement the same functions in the nginx configuration file include. htaccess file. For example, I want to add www.blogguy.cn. to migrate htaccess from apache to nginx, perform the following steps: Step 1: modify. the htaccess file, because there are some differences between the apache rewrite redirection rules and the nginx redirection rules. 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?
Related Article

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.