user782958
user782958

Reputation: 1

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

Many thanks

Upvotes: 0

Views: 1840

Answers (2)

Gavin C
Gavin C

Reputation: 88

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

Upvotes: 3

c00kiemon5ter
c00kiemon5ter

Reputation: 17604

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.

Upvotes: 2

Related Questions