2011-06-04 123 views
0

什么,我想实现的一个例子,我想重写mod_rewrite的动态URL

www.website.com/index.php
www.website.com

www.website.com/index.php?page=first
www.website.com/first

而且

www.website.com/index.php?category=cat &页=第二
www.website.com/cat/second

非常感谢

回答

2

尝试这样的事情

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    # you may want to check other uses of R 
    RewriteRule ^/index.php$/[R=301,L] 
    RewriteRule ^/index.php?page=(.*) /$1 [R=301,L] 
    RewriteRule ^/index.php?category=(.*)&page=(.*) /$1/$2 [R=301,L] 
</IfModule> 

您可能可以将最后两行放在一行中,播放正则表达式。 另外,请参阅this question,您可能需要查看ServerFault以获取更多信息。 许多问题都是这样的。

3

您是否100%确定您要重写这种方式吗?这似乎是通常设置重写的倒退。看看this questionfirst answer看看我的意思。

我要去无路可退,并要求也许你想在浏览器中样子的URL

http://www.website.com/cat/second 

以及您希望您的PHP应用程序到再收到

http://www.website.com/index.php?category=cat&page=second 

在这种情况下,你会想要像

<IfModule mod_rewrite.c> 
RewriteEngine On 
# note this first rewrite would be redundant in 99.999% of cases, apache expects to serve index.php for the/
RewriteRule ^/$ /index.php [last] 
RewriteRule ^(.*)$ /index.php?page=$1 [last] 
RewriteRule ^(.*)/(.*)$ /index.php?category=$1&page=$2 [nocase,last] 
</IfModule> 

如果我错了,那么Ivan c00kiemon5ter V卡纳克的答案会做你的权利。然后我也很好奇你在什么样的情况下想要做“反改写”:)

+0

我喜欢你的方法比我的更好。 +1从我 没有想到它,这样:)谢谢 – c00kiemon5ter 2011-06-07 20:43:07

+0

@Ivan c00kiemon5ter - 嘿谢谢。我很好奇@ user782958会告诉我们他们正在努力实现的目标。 – 2011-06-09 13:19:34