原始
localhost/edu/public/index.php
一. 消灭 localhost/edu/public/部分 通过配置虚拟主机实现
-
配置本地ip和域名的映射
C:\Windows\System32\drivers\etc 的 hosts 文件中
127.0.0.2 www.edu.com -
配置 127.0.0.2 所对应的网站根目录
D:\wamp\bin\apache\apache2.4.9\conf\extra 的 httpd-vhosts.conf 文件中
<VirtualHost 127.0.0.1:80>
DocumentRoot “d:/wamp/www”
<VirtualHost 127.0.0.2:80>
DocumentRoot “d:/wamp/www/edu/public”
然后还要到 httpd.conf 文件中启用上面配置的文件
#Include conf/extra/httpd-vhosts.conf 把#去掉即可。
二. 消灭index.php部分 通过 开启rewrite+配置.htaccess 协同完成
-
先到 httpd.conf 文件中开启rewrite
#LoadModule rewrite_module modules/mod_rewrite.so 把#去掉即可。
然后还要确保此文件中的对应www目录的 AllowOverride All (一般为默认) -
到项目根目录位置(项目入口所在位置)配置 .htaccess
DirectoryIndex index.php index.html index.htm(这一句也可以在虚拟主机中配置,那这里就不用了)
结果
只要输入 www.edu.com 即可访问项目入口了。
附:.htaccess 重写语法
解释版本
#这个规则实现 当访问 类似view-1.html 或者 page-1.html 的时候实际访问的是index.php还能带参数
RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
#不是目录或目录不存在
RewriteCond %{REQUEST_FILENAME} !-d
#不是文件或文件不存在 (这两个东西相当于两个判断条件,不能说把目录或者图片文件的访问也交给index.php处理吧)
RewriteCond %{REQUEST_FILENAME} !-f
#转给index.php处理,
#QSA:表示保留参数如get传值?xxx==xx…;
#PT:再把这个URL交给Apache处理;
#L表示是最后一个匹配项,不再往下匹配
#PT和L可加可不加
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
DirectoryIndex index.php
ps1:实际使用时,把中文注释全部去掉,不然会失效。
ps2:
RewriteCond %{HTTP_USER_AGENT} ^Mozilla//5/.0.*
RewriteRule index.php index.m.php
上面语句的作用是当你是用FF浏览器访问index.php这个文件的时候,会自动让你访问到index.m.php这个文件
使用版本
RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
DirectoryIndex index.php