例如原先路徑為: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 onRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]</IfModule>
完成以上四步就大功告成了。
註:發現在第三步中,將URL_MODEL=>1的時候也是可以的,我用的是thinkphp3.1