例如原先路径为:http://localhost/ehome/index.php/Index/index
现在想把index.php去掉,优化缩短url的长度,变为http://localhost/ehome/Index/index
下面介绍在thinkPHP框架中,Apache服务器对这种url优化的配置:
1:首先,找到你的Apache服务器的httpd.conf文件,在该文件中配置加载mod_rewrite.so模块,具体操作是在此文件中找到 #LoadModule rewrite_module modules/mod_rewrite.so,去掉前面的#即可,代码如下:
LoadModule rewrite_module modules/mod_rewrite.so
2:在httpd.conf文件中,修改Apache所指向的网站目录下的AllowOverride 的值,llowOverride none 改 AllowOverride ALL以我的为例:
<Directory "D:/wamp/www/">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride all<span style="color:#ffffff;">
</span>
#
# Controls who can get stuff from this server.
#
# onlineoffline tag - don't remove
Order Deny,Allow
Allow from all
Allow from all
</Directory>
3:配置为httpd.conf文件后,接下来就是配置项目的config.php文件了,把URL_MODEL的值改为2
'URL_MODEL' => 2, // URL模式——REWRITE模式
4:最后一步就是在网站的根目录下(与项目入口文件index.php同级)添加一个.htaccess文件,写相关配置,内容如下:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
注:发现在第三步中,将URL_MODEL=>1的时候也是可以的,我用的是thinkphp3.1