How to support. htaccess pseudo-static redirection on nginx

Source: Internet
Author: User
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?
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.