Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of RewriteCond rule parameters in Apache

Shulou Source: shulou.com Published: 2022-06-03 06:41:43 10月20日 Update

This article mainly shows you "what is the use of RewriteCond rule parameters in Apache", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "what is the use of RewriteCond rule parameters in Apache" this article.

RewriteCond is like the if statement in our program, which means that if one or more conditions are met, the RewriteRule statement next to RewriteCond will be executed. This is the most primitive and basic function of RewriteCond. For ease of understanding, let's take a look at a few examples.

The code is as follows:

RewriteEngine on

RewriteCond% {HTTP_USER_AGENT} ^ Mozilla//5/.0.*

RewriteRule index.php index.m.php

RewriteCond% {HTTP_USER_AGENT} ^ Lynx.*

RewriteRule index.php index.L.php

RewriteRule index.php index.b.php

The role of the above statement is that when you are using a FF browser to access the index.php file, it will automatically allow you to access the index.m.php file, when you are using some mobile terminal access, the actual access to the index.php file will be index.L.php, and if you are using other browsers, it will allow you to skip to index.b.php. To put it figuratively, the above statement is equivalent to the following statement in the program (take the PHP statement as an example):

The code is as follows:

If ($_ SERVER ['HTTP_USER_AGENT'] = =' Mozilla/5.0')

{

/ / Jump to access index.m.php

}

Else if ($_ SERVER ['HTTP_USER_AGENT'] = =' Lynx')

{

/ / Jump to access index.L.php

}

Else

/ / Jump to access index.b.php

Let's look at example 2:

RewriteCond% {HTTP_REFERER} (www.test.cn)

RewriteRule (. *) $test.php

The effect of the above statement is that if the host address of the last page you visited is www.test.cn, no matter which page you are currently visiting, you will jump to test.php access.

Let's look at example 3:

The code is as follows:

RewriteCond% {REMOTE_HOST} ^ host1.* [OR]

RewriteCond% {REMOTE_HOST} ^ host2.* [OR]

RewriteCond% {REMOTE_HOST} ^ host3.*

RewriteRule (. *) $test.php

The purpose of the above statement is to skip to test.php if your address is host1 or host2 or host3. As you can see here, the default between RewriteCond statements is AND, and if you want to OR, write it explicitly.

Here are some useful rewriting rules from your collection:

RewriteCond% {REQUEST_FILENAME}!-f / / if the file exists, access the file directly without the following RewriteRule. (rewrite is not performed if the file or file does not exist)

RewriteCond% {REQUEST_FILENAME}!-d / / # if the directory exists, access the directory directly without RewriteRule

RewriteCond% {REQUEST_URI}! ^. * (/ .css | / .js | / .gif | / .png | / .jpg | / .jpeg) $/ / # if it is a file with these suffixes, access the file directly without Rewrite

The above is all the content of the article "what is the use of RewriteCond rule parameters in Apache". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: File statement rule time parameter code function content article address browser directory program face page learning help browse original host Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi vpn Microsoft macOS