Linux nginx supports .htaccess file to implement pseudo-static method!

  

The method is as follows:


1. Create a new .htaccess file in the directory where you need to use the .htaccess file, such as my Discuz forum directory:

Vim /var/www/html/168pc/bbs/.htaccess

2. Enter the rules in it, here I enter the pseudo-static rules of Discuz:

# nginx rewrite rulerewrite ^(.* )/archiver/((fid| Tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;rewrite ^(.*)/forum-([0-9]+)-([0-9]+). Html$ $1/forumdisplay.php?fid=$2&page=$3 last;rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9 ]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;rewrite ^(.*)/profile-(username| Uid)-(.+).html$ $1/viewpro.php?$2=$3 last;rewrite ^(.*)/space-(username| Uid)-(.+).html$ $1/space.php?$2=$3 last;rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;# end Nginx rewrite rule

wq save and exit.

3. Modify the nginx configuration file:

vim /etc/nginx/nginx.conf

4. Introduced in the server{} where you need to add a pseudo-static virtual host The .htaccess file, as shown:


include /var/www/html/168pc/bbs/.htaccess;(change this to the specific location of your .htaccess file) Br>

wq save and exit.

5. Reload the nginx configuration file:

/etc/init.d/nginx reload

Reopen the webpage and see if your pseudo-static is normal. The rule syntax is correct. Normal, finished!

Supplement: I found on the Internet that I can automatically convert Apache Rewrite pseudo-static rules to Nginx Rewrite pages online. You can give it a try.

http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

The contents of this address contain functions that can be modified as described above. It is to automatically convert the rules in .htaccess to the rules available under nginx.

Summary: The .htaccess file is originally a Apache-specific distributed configuration file that provides a way to change the configuration for each directory, that is, to place a file containing instructions in a specific directory. This directory and all its subdirectories. In fact, modify it, nginx can also use .htaccess file to achieve a variety of functions. Implementing pseudo-static is just one of the uses of .htaccess. .htaccess can also do a lot of purposes, such as filtering access IP, setting web directory access rights, passwords, and so on.

Copyright © Windows knowledge All Rights Reserved