目的:使得URL地址看起來能更舒服些
實現:用統一的規則來簡化URL地址
本質:正則替換
適用:URL傳參不是很多,且個數一致
具體操作如下:
1.修改apache的配置文件httpd.conf
1).去掉前面的#號注釋LoadModule rewrite_module modules/mod_rewrite.so
2).
<Directory />
Options FollowSymLinks
AllowOverride None //None改為All
Order deny,allow
Deny from all
</Directory>
2. 在相應目錄下新建一“.htaccess”文件,可用editplus另存為。注意是:沒有名字,后綴為htaccess。如“.htaccess”在web根目錄下則對所有鏈接有效。
3. 編寫“.htaccess”
rewriteengine on #開啟引擎
RewriteRule ^test/(\w+)$ test.php?var=$1
RewriteRule ^test/(\w+)-(\w+)$ test.php?$1=$2
RewriteRule ^\d+$ test.php
對應test.php
<?php
var_dump($_GET);
?>
如報錯“Internal Server Error”,先檢查apache的rewrite是否開啟,然后檢查“.htaccess”是否有語法錯誤,如“RewriteRule ^\8+$ test.php”就會報此錯。
或加上以下:
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
下班了,就記到這,改天補充。