2013-02-11 70 views
0

我如何:如何做URL掩码?

http://ecorustic.com/model.php?category=Destinations&name=destindere&id=138

显示为:

http://ecorustic.com/Destinations/destindere

给用户,但在页面仍指向:

http://ecorustic.com/model.php?category=Destinations&name=destindere&id=138

? 我试着下面的代码,但链接保持不变:

RewriteEngine On 
RewriteRule ^[A-Za-z-]+/[A-Za-z-]+/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ model.php?category=$1&name=$2&id=$3 
+5

通过什么魔法我们应该推断ID? – TRiG 2013-02-11 12:04:22

+0

138可以从数据库中的“destindere”派生吗?如果是这样,那么可以这样做 - 第一步是删除model.php URL中的“id”查询字符串要求,并在内部执行查找。 – halfer 2013-02-11 12:05:15

+0

页面解析时给出id,它是数据库中页面的ID – Johny 2013-02-11 12:06:20

回答

1

尝试

RewriteEngine on 
RewriteCond %{REQUEST_URI} ^/(.*)/(.*)/(.*)$ 
RewriteRule^model.php?category=%1&name=%2&id=%3 [L] 

样品

http://ecorustic.com/Destinations/destindere/138

改写为

http://ecorustic.com/model.php?category=Destinations&name=destindere&id=138

+0

我需要: http://ecorustic.com/model.php?category = Destinations&name = destindere&id = 138将被转换为http://ecorustic.com/Destinations/destindere/138而不是其他方式 – Johny 2013-02-11 13:27:40

1

你应该看看the HTTP URL rewriting guide在Apache。你说:

我没有,但它给了我想要的确切oposite,它改变了 第二个链接进入第一:(,在所有的例子,我发现

这是真实,这正是你想要的东西:让漂亮的网址出现给用户,而在后台将其转换为不好的网址以供服务器读取。

有几千个教程和指南可以满足这些需求。

+0

是的,但是如何使这个奇特的URL出现?这就是我没有得到的因为我已经有了“坏”的网址,在这些教程中它解释了如何根据花哨的网址获得“坏”的网址,但是我需要基于“坏”网址(访问者需要的网址看到是花哨的,但他们看到了“坏”的网址); – Johny 2013-02-11 13:36:34

+0

@Johny,你没有*得到*的网址,它是'mod_rewrite'在* bad * url中翻译的花哨的URL。你会写'host.com/post/1 /'它会被重定向到'host.com?page = post&id = 1'。要做到这一点,你必须学习如何使用mod来重写Apache。已发布。 – Shoe 2013-02-11 14:39:26