Apache virtual host and rewrite configuration and rewrite rule interpretation

Source: Internet
Author: User

How to use rewrite Steps :

1. Configure Apache, enable rewrite mechanism, modify httpd.conf

in the httpd.conf enabled in rewrite

LoadModule Rewrite_module modules/mod_rewrite.so #如果没有这一行则在配置文件中添加这一行

2. Configure the virtual host

Modify Hosts file to add your IP and the Domain name correspondence, the following example

127.0.0.1 www.yaf.com

in the http.conf enable virtual Host in file

# Virtual hosts Enable virtual host

Include conf/extra/httpd-vhosts.conf#这一行是包含虚拟主机的配置文件

3. Modify the httpd_vhosts.conf file, configure the virtual host, and support rewrite

<direcotry > The configuration of the segment, the default value, is the httpd.conf in the configuration, look at the root directory .

<virtualhost *:80>

#ServerAdmin [email protected]

    # the root directory of the document

DocumentRoot "C:/MYENV/APACHE/HTDOCS/STATIC3"#根据项目实际情况填写

    # Domain name

ServerName www.hsp.com

Errorlog "Logs/dummy-host2.somenet.com-error.log"

#CustomLog "Logs/dummy-host2.somenet.com-access.log" common

    # Configuration rewrite Related Options ,

<directory "C:/MYENV/APACHE/HTDOCS/STATIC3" >

    # deny access to all

#Deny from all

Allow from all#允许所有访问

    # whether to display the list ( generally not enabled after publishing the project , for this configuration, for documentroot in htdos Outside of the directory is in effect )

#Options +indexes

    # is enabled rewrite

allowoverride all#启用rewrite

</Directory>

</VirtualHost>

4. A. htaccess file is added to the directory, this file is configured with the rewrite mechanism and rules, and the file is index.php in the same directory as the entry file of the project

<ifmodule rewrite_module>

# write about your rewrite rules

Rewriteengine on

#news-id (\d+) \.html$ is the rule news.php?id=$1 is the forwarded page

# multiple rules can be configured, the order of matching is from top to bottom

Rewriterule News-id (\d+) \.html$ news.php?id=$1

Rewriterule News-id (\d+) \.html$ error.php

</IfModule>

5. The rewrite rules are directly configured in the <direcotry > section.

<directory "C:/MYENV/APACHE/HTDOCS/STATIC3" >

    # deny access to all

#Deny from all

Allow from all

    # whether to display the list ( generally not enabled after publishing the project , for this configuration, for documentroot in htdos Outside of the directory is in effect )

#Options +indexes

    # is enabled rewrite

AllowOverride All

    # Sometimes, we can also put rewrite mechanisms and rules write here

#RewriteEngine on

#RewriteRule News-id (\d+) \.html$ news.php?id=$1

</Directory>


Remember to restart the Apache service after you have configured it.

Apache virtual host and rewrite configuration and rewrite rule interpretation

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.