2012-03-12 215 views
0

所以我聽說你可以使用htaccess來重寫網站的網址。比如我有一個PHP頁面,查看主題/帖子,看起來像這樣:htaccess重寫url

http://website.com/view.php?id=133

有人告訴我,我可以使它看起來像

http://website.com/view/133

我也有這個鏈接

http://website.com/delete.php?id=144

,我希望它看起來像這樣

http://website.com/delete/144

我怎麼會去這樣做呢?

謝謝。

+1

您閱讀手冊和比你做你想做的:http://httpd.apache.org/docs/2.4/ - 您要查找的功能是重寫模塊:http://httpd.apache.org/docs/current/mod/mod_rewrite.html初學者有[介紹](http://httpd.apache.org/docs/current/rewrite/intro.html)。 – hakre 2012-03-12 10:27:39

回答

0
RewriteRule ([a-z])/([0-9])\ $1.php?id=$2 

(即[az])將取代你的頁面的名稱([0-9])將取代你的ID分別在右側

請參閱

http://corz.org/serv/tricks/htaccess2.php

+0

但你不能夠在你的html頁面中使用相對路徑。例如:'http:// website.com/view/133'中的'css/style.css'應該是'view/css/style.css'。你將不得不爲你的CSS,js,img文件等使用絕對文件路徑。 – blackpla9ue 2012-03-12 10:38:23

0

是,你可以使用.htaccess來做所有的事情。

看看我的答案Redirecting localhost/xxx to localhost/xxx.php in WAMP

我希望它能幫助。

+0

我檢查了這些例子,但沒有爲我工作:L – 2012-03-12 10:46:44

+0

檢查mod_rewrite模塊是否已安裝並在您的apache安裝中啓用? – 2012-03-12 11:04:11

0
RewriteRule ^(.*)/([0-9]+)$ $1.php?id=$2 [NC,L] 

你必須給URL作爲http://website.com/delete/144,然後它會被重定向以144 id來delete.php。

例如,

<a href="http://website.com/delete/$id">Delete</a> 
+0

,並把它放在你網站根文件夾中名爲.htaccess的文件中。還包括RewriteRule行之前的'RewriteEngine On'行。 – Timm 2012-03-12 10:53:23