Apache's rewrite mechanism

Source: Internet
Author: User
Tags file url

When we use thinkphp, for example, we access the test method of a test controller, http://localhost/index.php/Test/test/1.html, the 1 is the parameter passed by the Get method, If this parameter is an ID, then we actually access the real address is http://localhost/index.php/Test/test? id=1.html, used thinkphp know, in fact, this is pseudo-static, by configuring the TP configuration file URL pattern can be achieved, but pseudo-static implementation, in fact, can also be implemented with Apache rewrite mechanism

Apache's implementation of the rewrite mechanism:

Like http://www.test.com/test.php?type=music&id=100.

Hope the above URL becomes

Http://www.test.com/test-music-id100.html

1. Enable the Rewrite module first

Modify the httpd.conf file in the Conf directory in Apache, locate LoadModule rewrite_module modules/mod_rewrite.so and open

2. Configure the virtual domain name

(1) Open Apache in the Conf directory under the VHOSTS.CONF, and introduce conf/extra/httpd-vhosts.conf, the code is as follows

Include conf/extra/httpd-vhosts.conf

(2) Modify the httpd-vhost.conf file, the code is as follows

<virtualhost *:80>

DocumentRoot "D:/phpstudy/www/test"

<directory "D:/phpstudy/www/test" >

</Directory>

</VirtualHost>

which

DocumentRoot is the site root directory

The Directory configuration node, which specifies the file or picture under the directory. Access rights, or set the error page of the virtual host, welcome page, etc.

(3) in the C:\WINDOWS\SYSTEM32\DRIVERS\ETC directory of the Hosts file, set the IP and host the corresponding relationship, the code is as follows

127.0.0.1 www.test.com

(4) Visit 127.0.0.1 www.test.com/test.php

Configuration is successful!

3. Configuring the rewrite mechanism

In general, httpd-vhost.conf files are not allowed to be arbitrarily configured by our programmers, so we can use. htaccess files for configuration

(1) Modify the httpd-vhost.conf file, open the use. htaccess file to match the rule, the code is as follows

AllowOverride All

(2) Create a. htaccess file at the root of the website, the code is as follows

<ifmodule rewrite_module>

#rewrite module Enabled

Rewriteengine on

#配置我们的rewrite规则

Rewriterule test-(\w+)-id (\d+) \.html$ test.php\?type=$1\&id=$2

</IfModule>

Where Rewriterule is matched using regular expressions, the matching part is the string following the domain name, or multiple rules can be configured at the same time (Rewriterule)

(3) Restart Apache and test (note, just modify.) htaccess is not required to restart Apache)

PHP code

Test results

There are a lot of common configuration about directory , because this time it is mainly the implementation of pseudo-static, so I will continue to organize a common configuration about directory next time.

  

Apache's rewrite mechanism

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.