开发者

.htaccess Mod Rewrite

I am trying to create a .htaccess mod rewr开发者_高级运维ite for an "infinite" amount of values. I mean 10 may be a good max number, but if I wish to increase then I would like for the script to be able to as well.

For instance:

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

All pages will have at least the "page" field, but not all my have extras and the extras may all be different names. However, I do not care about the field names. If it just redirects to: index.php?1=test&2=blah&3=6 then that would be the best.

I was thinking of possibly redirecting to something like below internally.

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

I am sorry if my post is a little confusing; I have tried to edit it.


Not exactly what you need but here is something that will give you idea on how to write recursive rules in mod_rewrite:

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

This redirects a URI of /hello/1/test/2/blah/3/foo/4 to index.php?foo=4&blah=3&test=2&hello=1.

Update Based on OP's comments:

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

This redirects a URI of /1/hello/2/test/3/blah/4/foo to /index.php?foo&blah&test&hello.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜