views:

74

answers:

1

I'm trying to learn Regex & URL Rewriting in PHP. How can I create an .htaccess file which will rewrite (not redirect) any url to index.php?q={url}?

For example:

http://www.example.com/baloon

to

http://www.example.com/index.php?q=baloon

I tried:

RewriteEngine On
RewriteRule ^$ index.php?q=$1 [R]

...but it is not working. What am I doing wrong?

Thanks.

+3  A: 

Rewriting ANY url to index.php?q=$1 would result in internal server error as it'd create an endless loop; instead do something like this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]
reko_t
Thanks, but it is redirecting it. Is there any solution so would stay in its url?
TTT
It shouldn't be redirecting it.
reko_t
You didn't add [R] in the rule right? If you did, then it'd redirect (as the other answer said incorrectly).
reko_t