Mod_rewrite dynamic URLS

こ雲淡風輕ζ 提交于 2019-12-11 23:31:51

问题


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

Many thanks


回答1:


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" :)




回答2:


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.



来源:https://stackoverflow.com/questions/6236091/mod-rewrite-dynamic-urls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!