在從事網管的工作其實遇到很多的問題反而是網頁設計師的問題= =a 但業主是不會理這種事情的

反正就是要解決問題!

今天遇到的是Rewrite的問題!

再Google一陣子之後看了很多「中文」的文章但是一直無法解決我的問題

直到向學長求救得到這篇超詳細淺顯易懂的文章 問題終於解決了

Link

 

重點大概是

1. 啟動apache 裡的Rewrite 模組

#LoadModule rewrite_module modules/mod_rewrite.so

前方的#去掉,啟動這模組

 

2. 寫好.htaccess 裡面的規則 其實大部分人卡關都是在這裡的正規式

其實就差不多了

 

這裡轉貼上面網站的簡單例子

htaccess檔裡面的程式↓

RewriteEngine On
RewriteRule ^user/(\w+)/?$ user.php?id=$1

 

開一頁PHP 檔名使用 user.php

 

<?php
// Get the username from the url
$id = $_GET['id'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Simple mod\_rewrite example</title>
    <style type="text/css"> .green { color: green; } </style>
</head>
<body>
  <h1>You Are on user.php!</h1>
  <p>Welcome: <span><?php echo $id; ?></span></p>
</body>
</html>

 

最後測試

輸入

http://127.0.0.1/user/NAME

即可得到

http://127.0.0.1/user.php?id=NAME

的結果

 

 

 

 

arrow
arrow
    全站熱搜

    andyyu0920 發表在 痞客邦 留言(0) 人氣()