打開apache的配置文件httpd.conf
找到
#LoadModule rewrite_module modules/mod_rewrite.so
把前面#去掉。沒有則添加,但必選獨占一行,使apache支持 mod_rewrite 模塊
找到
<Directory "D:/ApacheServer/web"> # # 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 None # # Controls who can get stuff from this server. # Order allow,deny Allow from all </Directory>
把
AllowOverride None 換成 AllowOverride All 使apache支持 .htaccess 文件
重啟apache服務器
在要啟用偽靜態的 PHP 項目根目錄下建立 .htaccess 文件
在 .htaccess 文件中輸入內容
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule index.html$ index.php RewriteRule index-([1-9]+[0-9]*).html$ index.php?p=$1 RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2 </IfModule>
注釋:
RewriteEngine 為重寫引擎開關,on為開啟,off為關閉。
RewriteRule 是路由轉向規則,之前路徑為瀏覽器中要輸入路徑,這里可以用正則表達式表達。之前路徑為瀏覽器中要輸入路徑,這里可以用正則表達式表達。+空格 后路徑為后台實際轉向路徑,
轉向后台實際路徑時可以傳參數,例子里的后台頁面可以用GET[′p′]GET[′p′]_GET[‘action’] GET[‘id′]來接收GET[‘id′]來接收1 代表瀏覽器路徑中輸入的第一個正則表達式的值,以此類推,$2代表第二個正則表達式的值
RewriteRule 路由轉向規則里正則表達式用括號 () 括起來
例子所在項目為test
在項目下 index.php 頁面內寫入內容
<?php if ($_GET ['p']) { echo "p : " . $_GET ['p']; } if ($_GET ['action']) { echo "action : " . $_GET ['action']; } if ($_GET ['id']) { echo "id : " . $_GET ['id']; } ?>