2012-03-12 205 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