Apache Rewrite rules

Source: Internet
Author: User

What is a virtual host? That is to make your own local development of the machine into a virtual domain name, such as: you in the development of a project under the PPTV 127.0.0.1/pptv_trunk, you want to turn your own computer domain name into www.pptv.com. Then your own machine access mode becomes a virtual domain name.

How to configure it. Step by step:

1. Open the Apache configuration file htppd.cnf. Open rewrite extensions and virtual host extensions, respectively:

LoadModule Rewrite_module modules/mod_rewrite.so This sentence before the comment # removed

Include conf/extra/httpd-vhosts.conf This sentence before the comment # removed, the key point

window and Linux are almost slightly different. There are a few examples in the file, we first make one, in fact very simple.

Namevirtualhost *:80

<virtualhost *:80>

DocumentRoot " D:/wamp/www/pptv_trunk "

ServerName www.pptv.com

<directory "D:/wamp/www/pptv_trunk>

Options Indexes followsymlinks

AllowOverride All

Order Allow,deny

Allow from all

</Directory>

</VirtualHost>

OK, so a virtual domain name is configured on Apache.

3. Open the hosts configuration file for Windows/linux, which is the DNS routing file for the system:

Let's edit and bind the PPTV project, plus this one:

127.0.0.1 pptv.com www.pptv.com

Refresh Dns:ipconfig/flushdns

Virtual domain VirtualHost Configuration detailed

What is rewrite? is rewrite, rewrite the URL of the access connection, sometimes in order to access the URL concise point , such as:

Www.pptv.com/i/?user_id=123&time=123&from=web

I think this address is too long to be concise enough to rely on the Apache virtual domain rules to write it simple:

Www.pptv.com/i/123/123/web

Before studying rewrite, let's take a look at a detailed configuration interpretation of a virtuhost.

We still open just a virtual domain name, we talk about this, how to configure Rewirite

Namevirtualhost *:80

<virtualhost *:80>

documentroot "d:/wamp/www/testphp/"

ServerName php.iyangyi.com

Serveralias www.pptv.cn #可省略

ServerAdmin [email protected] #可省略

Errorlog Logs/dev-error.log #可省略

Customlog Logs/dev-access.log Common #可省略

ErrorDocument 404 logs/404.html #可省略

<directory "d:/wamp/www/testphp/" >

Options Indexes FollowSymLinks

AllowOverride All

Order Allow,deny

Allow from all

Rewriteengine on

Rewritecond%{request_filename}!-d

Rewritecond%{request_filename}!-f

Rewriterule ^ (. *) $ index.php/$1 [qsa,pt,l]

</Directory>

</VirtualHost>

We say one by one how to configure.

1. first, we need to declare the virtual domain package block , using the XML style, start and end the symbol corresponding. *.80 means to accept 80 ports of any IP, which is generally the case and does not change.

<virtualhost *:80>

***

</VirtualHost>

2. When we get in, it's a statement documentroot. This is the path to the project code. fill in the root directory where the code for our project is located d:/wamp/www/testphp/ .

3. ServerName This is our virtual domain name, but also the key to this modification.

4. Serveralias This is the alias of our virtual domain name, we can not, his appearance is that we want another domain name to be adjusted to this directory. For example www.pptv.cn we also want to jump here, we can do this, but only if www.pptv.cn also bind host 127.0.0.1

5. ServerAdmin here to fill the server administrator's mailbox, you can not, when the server fails, if you have configured the mailbox in advance, will be sent to the mailbox, or the error message displayed in the page. Generally we can not fill out.

6. Errorlog here to fill in the error log display path, when the access error occurred, it will be recorded here, note: Logs/dev-error.log This file path is the Apache installation directory under the logs directory. Can not.

For example, when we visit http://php.iyangyi.com/f.html, f.html is a non-existent file, and this time it will be recorded.

Recorded in the Apache/logs/dev-error.log:

[Wed Mar 11 11:14:23 2015] [ERROR] [Client 127.0.0.1] File does not exist:d:/wamp/www/testphp/f.html

7. Customlog here to fill in the access log, used to record every request access, can not. Note: Logs/dev-access.log This file path is the logs directory under the Apache installation directory. Remember: Add common after the path.

Like when we visited Http://php.iyangyi.com/f.html.

This visit was recorded in the Apache/logs/dev-access.log:

127.0.0.1--[11/mar/2015:11:14:23 +0800] "get/f.html http/1.1" 404 177

8. ErrorDocument here to fill in 403,404 Error information adjustment page, used to access the appearance of 404 pages and other situations when the error page display, more useful, or not. Note:/404.html This file path is the root directory of the project, not the Apache directory.

I put it here in d:/wamp/www/testphp/404.html, so when we visit a nonexistent file, we automatically jump to this 404.html page.

For example, when we visit http://php.iyangyi.com/f.html, we will display 404.html content:

Therefore, we can use the usual status code according to the business need:

ErrorDocument 403/403.html

ErrorDocument 404/404.html

ErrorDocument 405/405.html

ErrorDocument 500/500.html

ErrorDocument 503/503.html

9. <directory "d:/wamp/www/testphp/" > * * * * This is the most important step, this is the path to fill in the project, and then all the rewrite rules are done inside. So this is very important. (rewrite rules are set within <directory>)

10. We go to the <Directory> level, many of which are critical. We mainly look at some of the commonly used, but also very critical. Options Indexes FollowSymLinks This is to set whether to display a directory listing of the root directory of the file .

Set to: Options Indexes followsymlinks: I visit php.iyangyi.com, if there is index.html (index.php) in the root directory of the file, the browser will display Index.html content, if there is no index.html, the browser will display the directory list of the root directory of the file, the directory list includes files and subdirectories under the root directory.

In the end is the priority to show index.php or index.html have Apache configuration decision:

<ifmodule dir_module>

DirectoryIndex index.html index.htm index.php index.php3

</IfModule>

Whichever is in front, if there is this file in the directory, it will be shown first.

What if I don't want people to visit my directory structure? , you can get rid of this indexs, or this:-index is OK.

into: Options followsymlinks or Options-indexes followsymlinks

Visit php.iyangyi.com again, we have index.php and index.html deleted, refresh the browser, it will show:

Forbidden

You don't have permission to access/on the this server.

11. allowoverride All what's this all about? Other teaching materials are very complex, we say simple, is allowed under the root directory. htaccess plays a rewrite role, as we'll be saying below, we'll put a . htaccess text in the root directory, which can also act as a URL rewrite.

If you want to disable the. htaccess file under this root directory, you can: AllowOverride None.

12. Order Deny,allow allow to all 2 of these are generally combined together. Used to set access permissions, set which IPs can access the domain name, and which IP is forbidden to access.

So order is to set these 2 combinations of sorts, not case-sensitive, intermediate, separate, and cannot have spaces in between.
Order Deny,allow: Indicates the setting "Check the Forbidden setting first, no permission to set prohibited"

Order Allow,deny: Indicates the setting "Check Allow settings first, without setting all allowable prohibitions"

And the final access result has a second parameter to decide!

Deny from all deny from 127.0.0.1 forbidden IP, all means all
Allow from all allows access from 127.0.0.1 IPs, all means all

Let's look at a few examples of their 2 combinations.

This example:

Order Deny,allow

Deny from all

Indicates that the permission is checked first, and all prohibitions are not allowed. But without allow, it means that all visits have been banned unconditionally.

Order Deny,allow

Deny from all

Allow from 127.0.0.1

Above means only 127.0.0.1 access is allowed

Order Allow,deny

Allow from all

Deny from 127.0.0.1 192.168.1.51

Above means prohibit 127.0.0.1 and 192.168.1.51 access, others can!

So this combination can achieve a lot of filtering access effect.

Rewritecond and Rewriterule instruction format configuration detailed

It took a lot of time to tell the virtualhost inside some configuration parameters of the writing and function, followed by the focus of rewrite, 3 core things: Rewriteengine,rewritecond,rewriterule

Rewriteengine
This is rewrite's general switch , used to turn on whether to start the URL rewrite, want to open, like this can be:

Rewriteengine on

Rewritecond and Rewriterule
Represents the directive definition and matches a rule condition, let rewriterule to rewrite. Simply put, Rewritecond islike the IF statement in our program, which means that if one or a few conditions are met, the Rewriterule statement next to Rewritecond is executed, which is Rewritecond's most primitive and basic function.

Let's look at an example:

Rewriteengine on

Rewritecond%{http_user_agent} ^mozilla//5/.0.*

Rewriterule index.php index.m.php

The above matching rule is: if the match to the HTTP request Http_user_agent is mozilla//5/.0.* beginning, That is, when you access the index.php file with your Firefox browser, you will automatically get access to the index.m.php file.

Rewritecond and Rewriterule are the corresponding relationships between the upper and lower. can have 1 or several rewritecond to match a rewriterule

Rewritecond is generally used in this way

Rewritecond%{xxxxxxx} + regular Match condition

So what kind of data requests can rewritecond match?
It is used in the following ways: Rewritecond%{name_of_variable} REGX FLAG

Rewritecond%{http_referer} (www.test.cn)

Rewritecond%{http_user_agent} ^mozilla//5/.0.*

Rewritecond%{request_filename}!-f

Above are the common 3 most common HTTP header connections that are most commonly used to match requests.

Http_referer
This matches the address of the visitor, which is also found in PHP $_requrest, which can be used when we need to judge or restrict the source of the access.

Like what:

Rewritecond%{http_referer} (www.test.cn)

Rewriterule (. *) $ test.php

The function of the above statement is that if the host address of the previous page you access is www.test.cn, then regardless of which page you are currently accessing, you will be redirected to access to test.php.

For example, can also use http_referer anti-inverted chain, that is, restrict others to use my website pictures.

Rewritecond%{http_referer}!^$ [NC]

Rewritecond%{http_referer}!ww.iyangyi.com [NC]

Rewriterule \. (jpg|gif) http://image.baidu.com/[r,nc,l]

NC nocase The meaning, ignoring the case. The first sentence, it is necessary to have a domain name, the first sentence is to look at the domain name if it is not www.iyangyi.com, when accessing. jpg or. gif files, will automatically jump to the http://image.baidu.com/, very good to meet the requirements of the anti-theft chain.

Request_filename
This basically is the most used, think URL rewrite is the most used, it is matching the current access to the domain name file, which piece belongs to Request_filename? is a URL other than the host domain name.

http://www.rainleaves.com/html/1569.html?replytocom=265

This URL, then request_filename is html/1569.html?replytocom=265

Look at an example:

Rewritecond%{request_filename}!-f

Rewritecond%{request_filename}!-d

Rewriterule ^room/video/(\d+) \.html web/index\.php?c=room&a=video&r=$1 [qsa,nc,l]

-D is a directory. Determine if teststring is not a directory to do this:!-d
-F is a file. Determine if teststring is not a file can be this:!-f

This two-sentence statement Rewritecond means that the requested file or path does not exist, and if a file or path exists, it will return a file or path that already exists. This is commonly used in combination.

The above rewriterule means that the room/video/123.html, which begins with the opening, becomes web/index.php?c=room&a=video&r=123 .

$ = The first parameter to match .

Rewriterule notation and Rules

Rewriterule is used in conjunction with Rewritecond, it can be said that Rewriterule is the result of a successful match after Rewritecond, so it is important.

Let's take a look at Rewriterule's wording:

Rewriterule Pattern Substitution [flags]

Pattern is a regular match. Substitution is a matching substitution [flags] is some parameter limit;

Let's look at a few examples:

Rewriterule ^room/video/(\d+) \.html web/index\.php?c=room&a=video&r=$1 [qsa,nc,l]

It's like a room/video/123.html in the beginning of a web/index.php?c=room&a=video&r=123.

Rewriterule \. (jpg|gif) http://image.baidu.com/[r,nc,l]

It means that a file that accesses. jpg or GIF will be adjusted to http://image.baidu.com

Therefore, mastering the regular level is the key. Later, I will specialize in a regular chapter to learn.

What do you mean, we'll see [flags] again?

Because it's too much. I'll just pick out a few of the most commonly used to say.

[QSA] Qsappend (append query string) means that the secondary tag forces the rewrite engine to append a query string to an existing replacement string, rather than a simple replacement. You can use this tag if you need to add information to the request string through a rewrite rule. The example of the above-mentioned one must be used.

NC nocase (ignoring case) means that the pattern ignores case, that is, "A-Z" and "A-Z" do not differ when pattern matches the current URL. This is usually added because our URLs are not case-sensitive.

R Redirect (forced redirection) means that , after matching patter, substitution is an HTTP address URL, the situation is adjusted out. The above example, which is adjusted to image.baidu.com, must also be used.

L last (the end rule) means that it has been matched, stopped immediately, and no longer matches the rule below, similar to the break syntax in the programming language, jumping out.

Some other specific syntax, you can refer to the following information

Apache Rewrite rules

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.