Role of the. htaccess file in the PHP framework,

Source: Internet
Author: User

Role of the. htaccess file in the PHP framework,
1. Prerequisites for using the htaccess File

. The main function of htaccess is to rewrite the url, that is, when the browser accesses a folder on the server through the url, as the host, we can receive the url and how to receive it, is the role of this file. All accesses are implemented through URLs, so. htaccess has a small role. For this reason, websites generally use. htaccess to attract users through a very friendly url, and then use. htaccess to bring users to the desired location.

To use this powerful function, you must enable the rewrite module in apache.

As mentioned in the previous article, enabling the rewrite module on windows and ubuntu uses. htaccess.

In fact, the steps for enabling the module are the same, regardless of Windows and linux.

2. htaccess basic syntax

Enable rewrite engine: RewriteEngine on

Set the rewrite root directory: RewriteBase/-Description: Because this folder is defined, the corresponding replacement has a reference.

Match all requests that meet the condition: RewriteCond-Note: RewriteCond defines a series of Rule conditions. This command can have one or more rules. Our. htaccess is started, otherwise the user will directly access the desired directory.

For example, in order to allow search engines to crawl more of our web pages and avoid repeated capturing, we usually redirect domain names without www to www.XXX.com, which is implemented as follows:

RewriteEngine On

RewriteCond % {HTTP_HOST} ^ nbphp \. com $ [NC]

RewriteRule ^ (. *) $ http://www.nbphp.com/#1 [R = 301, L]

In the preceding example, nbphp.com is redirected to www.nbphp.com.

% {HTTP_HOST} is the primary domain name that obtains the URL accessed by the user, followed by a regular expression matching after the space, and the consciousness is whether it is nbphp.com.

If the URL used by the user to access meets all the conditions proposed by RewriteCond listed, the next RewriteRule will start to guide the implementation of the important functions of the. htaccess file.

Similarly, the regular expression is used to analyze the user's URL except the primary domain name nbphp.com. ^ (. *) $ indicates all content. Then, the space is followed by the directory we guided the user to access. We took him to a new domain name. $1 indicates the content obtained by matching the url in the brackets.

This is a complete small example. For details about how to call a part of a url in RewriteCond, refer to this article (Apache Mod_rewrite Learning (RewriteCond Rewrite Rules );

 

File Source: http://www.cnblogs.com/yyl8781697/articles/php-htaccess.html

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.