Linux | apache虚拟主机和rewrite配置规则解释

  • 内容
  • 相关

1.配置apache,启用rewrite机制,修改httpd.conf

httpd.conf中启用rewrite

LoadModule rewrite_module modules/mod_rewrite.so

#如果没有这一行则在配置文件中添加这一行

2.配置虚拟主机

修改 hosts文件添加你的 ip和域名对应,如下事例

127.0.0.1       www.fishyoung.com

http.conf文件中启用虚拟主机

# Virtual hosts 启用虚拟主机

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

3.修改 httpd_vhosts.conf文件,配置虚拟主机并支持rewrite

<Direcotry >段的配置,默认值,是在httpd.conf的中配置,看根目录.

<VirtualHost *:80>

    #ServerAdmin webmaster@fishyoung.com

    #文档的根目录,根据实际情况填写

    DocumentRoot "/etc/httpd/apache/htdocs/static3"   

    #域名

    ServerName www.fishyoung.com

    ErrorLog "logs/fishyoung.com-error.log"

    #CustomLog "logs/fishyoung.com-access.log" common

    #配置rewrite相关选项,

    <Directory "/etc/httpd/apache/htdocs/static3">

    #拒绝所有的访问

    #Deny from all

    #允许所有访问

    Allow from all     

    #是否显示列表(在发布项目后一般是不启用,对于这个配置,针对 DocumentRoothtdos外的目录生效)

    #Options +indexes

    #是否启用rewrite

    Allowoverride  all   #启用rewrite

    </Directory>

</VirtualHost>

4.在目录下添加了一个 .htaccess  文件,这个文件时配置rewrite机制和规则,这个文件要和项目的入口文件index.php在同一个目录下

<IfModule rewrite_module>

#写你的rewrite规则

RewriteEngine On

#news-id(\d+)\.html$是规则  news.php?id=$1是转发的页面

# 可以配置多个规则,匹配的顺序是从上到下

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

RewriteRule  news-id(\d+)\.html$     error.php

</IfModule>

5.这个rewrite的规则也直接在 <Direcotry > 段直接配置

<Directory "/etc/httpd/apache/htdocs/static3">

    #拒绝所有的访问

    #Deny from all

    #允许所有的访问

    Allow from all

    #是否显示列表(在发布项目后一般是不启用,对于这个配置,针对 DocumentRoothtdos外的目录生效)

    #Options +indexes

    #是否启用rewrite

    Allowoverride  all

    #我们有时候,也可以把rewrite机制和规则写这里

    #RewriteEngine On

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

    </Directory>

配置好后要记得重启apache服务

 您阅读这篇文章共花了:

上一篇:Linux | CentOS 7单机快速安装OpenStack

下一篇:Linux | Apache环境下强制http跳转至https的配置总结

本文标签:    

版权声明:本文依据CC-BY-NC-SA 3.0协议发布,若无特殊注明,本文皆为《fishyoung》原创,转载请保留文章出处。

本文链接:Linux | apache虚拟主机和rewrite配置规则解释 - http://www.fishyoung.com/post-232.html