前段時間項目為了配合seo的工作,把現有的php網站改成靜態頁面,剛拿到需求時候第一感覺就是用靜態頁面啊,可是看了一會以后發現頁面有點多4、50個,沒辦法就用比較簡單的url重寫(apache的)吧,
去掉這個前面的#,啟用它
LoadModule rewrite_module modules/mod_rewrite.so
AllowOverride None 的None 改成All
然后就是寫.htaccess文件了(放在根目錄 但是會犧牲微量的訪問時間),當然也可以在apache的配置文件中加,但是那個不太靈活(犧牲的時間更短)
實例
#打開重寫語句
RewriteEngine On
RewriteRule castrolmagnatec.com.cn/questions/([a-z]+)/(index.html)?$ castrolmagnatec.com.cn/questions/index.php?tag=$1
分析
castrolmagnatec.com.cn/questions/([a-z]+)/(index.html)?$為需要匹配的靜態url的正則表達式
castrolmagnatec.com.cn/questions/index.php?tag=$1后面的$1為正則表達式中第一個分組(圓括
號)中匹配的內容
上面的代碼可以實現下面的效果
把 域名/castrolmagnatec.com.cn/questions/aaaaa/index.html 的url跳轉到
域名/castrolmagnatec.com.cn/questions/index.php?tag=aaaaa
效果就是php的頁面在瀏覽器上顯示的url為
域名/castrolmagnatec.com.cn/questions/aaaaa/index.html