Learning Apache Rewrite syntax based on the. htaccess file in ZF

Source: Internet
Author: User

First, let's take a look at the. htaccess file:

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^.*$ - [NC,L]RewriteRule ^.*$ index.php [NC,L]

Three commands are involved:

1. RewriteEngine is the simplest and controls the switch of the redirection engine. The first command indicates opening the redirection engine.

2. The RewriteCond command is slightly more complex. Each Command defines a condition. Only when this condition is met Will the RewriteRule following this command take effect.Note: This command only provides conditions for the subsequent RewriteRule command!For example, the second RewriteRule command in the above file does not need to be executed only when the previous RewriteCond command meets the requirements.

If you want to judge multiple conditions at the same time, you can add the [OR] mark to the end of the judgment, indicating that the judgment is not terminated for the time being. Continue to read the next RewriteCond command.

The second parameter of this command is the value to be determined. Here is the server variable % {REQUEST_FILENAME}

The third parameter is the matching mode. The following match modes are used in the file:

-S: determines whether the second parameter is a regular file on the server with a size greater than 0;

-L determine whether the second parameter exists on the server and the symbolic link file;

-D: determines whether the second parameter is a folder on the server;

If any of the three rules is met, the first RewriteRule command is executed.

3. Let's look at the RewriteRule command.

The second parameter of the RewriteRule command is a regular expression. The Regular Expression of the first RewriteRule command will be applied to the request URL (urlencode); then the RewriteRule command will apply the regular expression to the result of the previous command.

The third parameter of this command is the value to be replaced. -This parameter is left unchanged and is not replaced.

This command can also be followed by a flag. For example, the NC mark means no case, and the L mark means to stop the execution of the following RewriteRule command.

Therefore, the ZF file means that if it is a file on the server, do not redirect it. If the file in the Request Path does not exist, redirect it to index. php.

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.