开发者

Mod_rewrite dynamic URLS

An example of what I am trying to achieve, I want to rewrite

www.website.com/index.php

www.website.com

and

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

www.website.com/first

but also

www.w开发者_StackOverflow社区ebsite.com/index.php?category=cat&page=second

www.website.com/cat/second

Many thanks


Are you 100% sure you want to do the rewriting this way? It seems like this is backwards to how you usually setup rewrites. Check out this question and the first answer to see what I mean.

I'm going to go out on a limb and ask that maybe you want to have the URLs in the browser look like:

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

and that you want your php application to then receive:

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

In which case you would want something like

 <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>

If I'm wrong, then Ivan c00kiemon5ter V Kanak's answer will do you right. And I'm then also curious about what kind of situation you are in where you want to do "anti-rewriting" :)


try something like this

<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>

You can probably fit the last two in one line, playing the regexes. Also, see this question and you may want to look up ServerFault for more. Many questions like this around.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜