


A brief discussion on the difference between apache and nginx rewrite_PHP tutorial
1. Nginx Rewrite rule related instructions
The instructions related to Nginx Rewrite rules include if, rewrite, set, return, break, etc., among which rewrite is the most critical instruction. A simple Nginx Rewrite rule syntax is as follows:
rewrite ^/b/(.*).html /play.php?video=$1 break;
If you add an if statement, the example is as follows:
if (!-f $request_filename)
{ rewrite ^/img/(.*)$ /site/$host/images/$1 last; }
2. Comparison of Rewrite rule examples between Nginx and Apache
There is not much difference between simple Nginx and Apache rewrite rules, and they are basically fully compatible.
Apache Rewrite rules:
RewriteRule ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 [L]
RewriteRule ^/ceshi/$ /zl/ceshi.php [L]
RewriteRule ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 [L] RewriteRule ^/pingce([0-9]*)/ $ /zl/pingce.php?id=$1 [L]
Nginx Rewrite rules:
rewrite ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 last;
rewrite ^/ceshi/$ /zl/ceshi.php last;
rewrite ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 last;
rewrite ^/pingce([0-9]*)/$ /zl/pingce.php?id=$1 last;
It is not difficult to find that it is quite simple to change Apache's Rewrite rules to Nginx's Rewrite rules. After changing the rules, use the "nginx -t" command to check and find that the nginx.conf configuration file has syntax errors, then you can try to add conditions quotation marks. For example, the following Nginx Rewrite rule will report a syntax error:
rewrite ^/([0-9]{5}).html$ /x.jsp?id=$1 last; add quotation marks and it will be correct:
rewrite “^/([0-9]{5}).html$” /x.jsp?id=$1 last;
There are subtle differences between the Rewrite rules of Apache and Nginx when URL jumps:
Apache Rewrite rules:
RewriteRule ^/html/tagindex/([a-zA-Z]+)/.*$ /$1/ [R=301,L]
Nginx Rewrite Rule:
rewrite ^/html/tagindex/([a-zA-Z]+)/.*$ http://$host/$1/ permanent;
In the above example, we noticed that "http://$host" was added to the replacement string of the Nginx Rewrite rule, which is required in Nginx.
In addition, the Rewrite rules of Apache and Nginx are also different in variable names, for example:
Apache Rewrite rules:
RewriteRule ^/user/login/$ /user/login.php?login=1&forward=http://%{HTTP_HOST} [L]
Nginx Rewrite Rules:
rewrite ^/user/login/$ /user/login.php?login=1&forward=http://$host last;
The correspondence between some instructions and tags with the same or similar functions between Apache and Nginx Rewrite rules :
Apache's RewriteCond instruction corresponds to Nginx's if instruction;
Apache's RewriteRule instruction corresponds to Nginx's rewrite instruction;
Apache's [R] tag corresponds to Nginx's redirect tag;
Apache's [P] The tag corresponds to Nginx's last tag;
Apache's [R, L] tag corresponds to Nginx's redirect tag;
Apache's [P, L] tag corresponds to Nginx's last tag;
Apache's [PT, L ] tag corresponds to Nginx’s last tag;
Specified domain names are allowed to access this site, other domain names will jump to http://www.aaa.com:
Apache Rewrite Rules:
RewriteCond %{HTTP_HOST} ^(.*?).domain.com$
RewriteCond %{HTTP_HOST} !^qita.domain.com$ RewriteCond %{DOCUMENT_ROOT}/market/%1/index.htm -f
RewriteRule ^/wu/$ /market/%1/index.htm [L]
Nginx’s if instruction does not support nesting, nor does it support multi-condition matching such as AND and OR. Compared with Apache's RewriteCond seems a little more troublesome, but we can implement this example through the Nginx configuration writing method on the next page:
Nginx Rewrite rules:
if ($host ~* ^(.*?).domain.com$)
{
set $var_wupin_city $1;
set $var_wupin ‘1′;
}
if ($host ~* ^qita.domain.com$)
{
set $var_wupin ‘0′;
}
if (!-f $document_root/market/$var_wupin_city/index.htm)
{
set $var_wupin ‘0′;
}
if($var_wupin ~ ‘1′)
{
rewrite ^/wu/$ /market/$var_wupin_city/index.htm last;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Installing Docker images offline requires the following steps: 1. Obtain the mirror TAR file; 2. Export the mirror file; 3. Transfer the mirror file; 4. Import the mirror file; 5. Verify the mirror installation.

Created by Ripple, Ripple is used for cross-border payments, which are fast and low-cost and suitable for small transaction payments. After registering a wallet and exchange, purchase and storage can be made.

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.
