【问题标题】:Mod_rewrite dynamic URLSmod_rewrite 动态 URL
【发布时间】:2011-06-04 10:06:46
【问题描述】:

我想要实现的一个例子,我想重写

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&page=second
www.website.com/cat/second

非常感谢

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:

    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 Kanak 的回答会是对的。然后我也很好奇你在什么样的情况下想做“反重写”:)

    【讨论】:

    • 我比我更喜欢你的方法。 +1 我没想到,这样 :) 谢谢
    • @Ivan c00kiemon5ter - 嘿,谢谢。我很好奇@user782958 会告诉我们他们想要达到的目标。
    【解决方案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 以了解更多信息。 这样的问题很多。

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 2014-03-08
      • 1970-01-01
      • 2013-05-14
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2015-08-19
      相关资源
      最近更新 更多