1. 程式人生 > >thinkPHP Apache 優化url,隱藏index.php所需配置

thinkPHP Apache 優化url,隱藏index.php所需配置

例如原先路徑為: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