Home Backend Development PHP Tutorial A brief discussion on the difference between apache and nginx rewrite_PHP tutorial

A brief discussion on the difference between apache and nginx rewrite_PHP tutorial

Jul 21, 2016 pm 03:13 PM
apache nginx re rewrite the difference and instruction have of Related rule

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;

}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326583.htmlTechArticle1. Nginx Rewrite rule related instructions Nginx Rewrite rule related instructions include if, rewrite, set, return, break, etc. Among them, rewrite is the most critical instruction. A simple Nginx Rewrite rule...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

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".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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).

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

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]

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

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.

How to install mirror offline in docker How to install mirror offline in docker Apr 15, 2025 am 11:36 AM

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.

What currency does Ripple (XRP currency) belong to? Detailed tutorial for beginners What currency does Ripple (XRP currency) belong to? Detailed tutorial for beginners Apr 28, 2025 pm 07:57 PM

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.

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

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...

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

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.

See all articles