【问题标题】:.htaccess Mod Rewrite.htaccess Mod 重写
【发布时间】:2011-07-02 05:52:24
【问题描述】:

我正在尝试为“无限”数量的值创建 .htaccess mod 重写。我的意思是 10 可能是一个很好的最大数字,但如果我想增加,那么我希望脚本也能够增加。

例如:

From: http://www.domain.com/index.php?page=test
To:   http://www.domain.com/test

From: http://www.domain.com/index.php?page=test&act=blah
To:   http://www.domain.com/test/blah

From: http://www.domain.com/index.php?page=test&act=blah&id=6
To:   http://www.domain.com/test/blah/6

所有页面都至少有“页面”字段,但并非所有页面都有附加项,附加项可能都是不同的名称。但是,我不关心字段名称。如果它只是重​​定向到:index.php?1=test&2=blah&3=6 那就最好了。

我在考虑可能会在内部重定向到下面的内容。

From: http://www.domain.com/index.php?page=test/blah
To:   http://www.domain.com/test/blah

如果我的帖子有点混乱,我很抱歉;我试图编辑它。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    不完全是您需要的,但这里有一些内容可以让您了解如何在 mod_rewrite 中编写递归规则:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)(/.*)?$ $3?$1=$2 [N,QSA,DPI]
    RewriteRule ^(/[^/]+|[^/]+/|/?)$ /index.php [L,QSA,DPI]
    

    这会将/hello/1/test/2/blah/3/foo/4 的URI 重定向到index.php?foo=4&blah=3&test=2&hello=1

    基于 OP 的 cmets 更新:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)(/.*)?$ $3?$2 [N,QSA,DPI]
    RewriteRule ^(/[^/]+|[^/]+/|/?)$ /index.php [L,QSA,DPI]
    

    这会将/1/hello/2/test/3/blah/4/foo 的URI 重定向到/index.php?foo&blah&test&hello

    【讨论】:

    • 我不想要 URL 中的数字。我希望 URL 看起来像这样,但没有数字。
    • 请重新阅读我的原帖。我正在寻找 /hello/test/blah/foo/ 重定向到 index.php?1=hello&2=test&3=blah&4=foo。你倒过来了。
    • @Flipper:mod_rewrite 并不是真正的编程代码,你不能真正增加其中的计数器(至少我不知道)。如果原始 URI 中有 1、2、3、4,那么当然可以在查询字符串中使用。
    • 有道理。我刚刚将所有内容重新路由到 index.php?something= 然后使用 PHP 将其全部拆分。不过还是谢谢。解决了我的问题:没有办法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多