1

At this moment I have a situation in which pretty url's are rewritten so that everything goes by the index.php, while maintaining the $_GET variables. The htaccess looks as follows:

RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3&type=$4
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2
RewriteRule ^([^/\.]+)/?$ index.php?p=$1

This works fine. However, I want it to work the other way around as well. If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123.

Is this possible, and if so, how? All I've managed so far is creating a redirect loop.

For those interested, the redirect loop I created looks as follows:

RewriteCond %{QUERY_STRING} ^p=([^&]+)
RewriteRule ^/?index\.php$ http://www.example.com/%1? [L,R=301]
anubhava
  • 664,788
  • 59
  • 469
  • 547
Sander Koedood
  • 4,967
  • 5
  • 22
  • 34
  • possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Jay Blanchard Sep 25 '14 at 12:37
  • 1
    look into existing router solutions (ZF, SF etc.). – fejese Sep 25 '14 at 12:39
  • @fejese: Could you give me a link of where I can read into that? I've never heard of that before. – Sander Koedood Sep 25 '14 at 12:49
  • For example http://symfony.com/doc/current/components/routing/introduction.html – fejese Sep 25 '14 at 13:51

1 Answers1

1

I want it to work the other way around as well. If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123

Yes it indeed possible but you will need to use THE_REQUEST variable for that.

RewriteCond %{THE_REQUEST} \s/+index\.php\?p=([^\s&]+)&projectid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]

You can build similar rule for other URLs as well.

anubhava
  • 664,788
  • 59
  • 469
  • 547