牛骨文教育服务平台(让学习变的简单)
博文笔记

apache 配置虚拟目录+默认页+隐藏index.php

创建时间:2016-05-27 投稿人: 浏览次数:4396

如果有多个项目文件夹,怎么配置让localhost指向其中的某个文件夹呢。

1.检查apache的配置文件httpd.conf,看Virtual hosts模块是否开启。

Include conf/extra/httpd-vhosts.conf 。前面的【#】需要去除。

2.打开httpd-vhosts文件, 这里由于80端口被占用了,所以用了8080

<VirtualHost *:8080>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "C:wampwwwmyproject"
    ServerName localhost
    ServerAlias localhost
    <IfModule dir_module>
        DirectoryIndex
  index.php
     </IfModule>

    ErrorLog "logs/dummy-host.example.com-error.log"
    CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

DocumentRoot:项目路径,上例为wamp里面的路径,apache中可以为/www/myproject/Web

ServerName:改为localhost,此配置也可以不需要的

DirectoryIndex:当输入localhost时,默认访问的页面

如果需要将127.0.0.1/index.php 改为通过127.0.0.1/index。来访问该怎么实现呢?

这里需要使用apache的Rewrite功能。

1.检查apache的配置文件httpd.conf,看rewrite_module模块是否开启。

LoadModule rewrite_module modules/mod_rewrite.so。前面的【#】需要去除。

2.设置httpd-vhosts文件内容

<VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{REQUEST_URI} !^/(css|js|images|ajax)/.*$
        RewriteRule ^(.+)$ /
index.php/$1
        ServerAdmin webmaster@dummy-host.example.com
        DocumentRoot "C:wampwwwmyproject"
        ServerName localhost
        ServerAlias localhost
        <IfModule dir_module>
            DirectoryIndex index.php
          </IfModule>
        ErrorLog "logs/dummy-host.example.com-error.log"
        CustomLog "logs/dummy-host.example.com-access.log" common
        </VirtualHost>

RewriteEngin:设置为【on】,表示路由开启

RewriteCond:路由条件,这里设置如果是访问(css,js,images,ajax)文件夹,则不需要路由

RewriteRule:除了路由条件里面排除的,访问其它的地址,都将跳转到index.php里操作

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。