(1) 启用rewrite模块,在默认状况下,没有启用php
修改httpd.conf文件html
#启动rewrite模块apache
LoadModule rewrite_module modules/mod_rewrite.so服务器
确认是否启动成功ide
<?php phpinfo();?>网站
(2) 配置咱们的虚拟主机htm
httpd.conf 打开虚拟主机的配置文件继承
# Virtual hosts图片
Include conf/extra/httpd-vhosts.confip
修改 httpd-vhost.conf
<VirtualHost *:80>
DocumentRoot "C:/myenv/apache/htdocs/static2"
#Directory配置节点,用于指定该目录下的文件或是图片.的访问权限
#设置虚拟主机的错误页面,欢迎页面
<Directory "C:/myenv/apache/htdocs/static2">
</Directory>
</VirtualHost>
(3) 在hosts文件中,配置ip和主机的对应关系
127.0.0.1 www.hsp.com
(4) 这时咱们访问 http//www.hsp.com/news.php
咱们能够访问到该页面.
☞ 一个重要的知识点:
在apache服务器中,若是某个文件夹,没有指定访问权限,则以上级目录的权限为准,若是他本身指定了访问权限,则以本身的为准.
请注意,在配置访问权限的时候,顺序很重要:
#Order allow,deny 表示先看allow ,在看deny,留下的就是能够访问
Order deny,allow
Deny from all
allow from 127.0.0.1
(5) 关于<Directory> 节点配置必须掌握
比较完整的配置文件
第一种配置方式
<VirtualHost *:80>
DocumentRoot "C:/myenv/apache/htdocs/static2"
#Directory配置节点,用于指定该目录下的文件或是图片.的访问权限
#设置虚拟主机的错误页面,欢迎页面
ServerName www.hsp.com
<Directory "C:/myenv/apache/htdocs/static2">
#这里能够指定是否让人访问
#Allow from all
#是否列出文件目录结构
# 若是但愿列出 indexes 不但愿 none
#Options indexes
#如何配置网站的首页面
DirectoryIndex abc.html abc2.html
#如何配置404错误页面,引导用户引入新页面
errorDocument 404 /404.html
#配置咱们的rewrite规则
RewriteEngine On
#rewrite的规则 若是 aaa.html 就跳转到news.php
#$1 表示反向引用,第一个子表达式的内容
#说明若是在正则规范中直接引用子表达式的内容,则使用\n
#若是是在后面由于,则使用$n
RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$ news.php?type=$1&id=$2
</Directory>
</VirtualHost>
特别说明: 容易犯的错误,必定要记住启用rewrite模块.
思考: 上面咱们配置都要去修改 httpd-vhost.文件,但管理员不给你这个权限,怎么办?
思路: 能够把配置,写到 .htaccess文件.
第二种配置方式: 即把一部分配置放在 http-vhost.conf 文件, 把rewrite 规则放在 .htaccess
<VirtualHost *:80>
DocumentRoot "C:/myenv/apache/htdocs/static2"
#Directory配置节点,用于指定该目录下的文件或是图片.的访问权限
#设置虚拟主机的错误页面,欢迎页面
ServerName www.hsp.com
<Directory "C:/myenv/apache/htdocs/static2">
#这里能够指定是否让人访问
#Allow from all
#是否列出文件目录结构
# 若是但愿列出 indexes 不但愿 none
#Options indexes
#如何配置网站的首页面
DirectoryIndex abc.html abc2.html
#如何配置404错误页面,引导用户引入新页面
errorDocument 404 /404.html
#若是你配置了allowoverride all 这表示到对应的目录的.htaccess去匹配规则
allowoverride all
</Directory>
</VirtualHost>
在对应的文件下 .htaccess文件
<IfModule rewrite_module>
#若是rewrite 模块启用
#配置咱们的rewrite规则
RewriteEngine On
#rewrite的规则 若是 aaa.html 就跳转到news.php
#$1 表示反向引用,第一个子表达式的内容
#说明若是在正则规范中直接引用子表达式的内容,则使用\n
#若是是在后面由于,则使用$n
RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$ news.php?type=$1&id=$2
#RewriteRule aaa.html news.php
</IfModule>
请注意: 项目中的 .htaccess文件的配置也是继承管理
第三种配置方法:
http-vhost.conf
<VirtualHost *:80>
DocumentRoot "C:/myenv/apache/htdocs/static2"
#Directory配置节点,用于指定该目录下的文件或是图片.的访问权限
#设置虚拟主机的错误页面,欢迎页面
ServerName www.hsp.com
<Directory "C:/myenv/apache/htdocs/static2">
#若是你配置了allowoverride all 这表示到对应的目录的.htaccess去匹配规则
allowoverride all
</Directory>
</VirtualHost>
.htacces文件
#这里能够指定是否让人访问
#Allow from all
#是否列出文件目录结构
# 若是但愿列出 indexes 不但愿 none
#Options indexes
#如何配置网站的首页面
DirectoryIndex abc.html abc2.html
#如何配置404错误页面,引导用户引入新页面
errorDocument 404 /404.html
<IfModule rewrite_module>
#若是rewrite 模块启用
#配置咱们的rewrite规则
RewriteEngine On
#rewrite的规则 若是 aaa.html 就跳转到news.php
#$1 表示反向引用,第一个子表达式的内容
#说明若是在正则规范中直接引用子表达式的内容,则使用\n
#若是是在后面由于,则使用$n
RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$ news.php?type=$1&id=$2
#RewriteRule aaa.html news.php
</IfModule>