Htaccess rewrite

I want to rewrite some url using .htaccess.

eg: Source url


in to this form (1st form)


OR this form (2nd form)


I ve used the following code for this to achieve the first form.

RewriteEngine On
RewriteBase /demo/

RewriteRule ^(.*)$ 	 index.php?q=$1 

Now the request is going to index.php, but my question in how can get the new rewritten url. ie, http://localhost/demo/index.php?q=a/b.

$_SERVER[‘QUERY_STRING’] contains “q=demo.php” which mean nothing to me.

Please Help me… how can i do the rewriting. Ive googled and got some codes but not working. I need code for both form and want to know how can i access the both rewritten url.

Thanks in advance…

Hi tanithin. Welcome to SitePoint!

We’ve moved this question to a more appropriate forum. :slight_smile:

It’s not entirely clear to me what you’re asking here.
When does $_SERVER[‘QUERY_STRING’] contain “q=demo.php” ?
Why are you using $_SERVER[‘QUERY_STRING’] and not $_GET ?

Also, are you using Redirect or RedirectMatch anywhere? If not, drop the RewriteBase.

I found the problem with my code it was “looping in htaccess rewrite”. Solved it by puttng RewriteCond %{ENV:REDIRECT_STATUS} ^$.

I want to display the new redirected path ie, “http://localhost/demo/index.php?q=a/b” for the request “http://localhost/demo/a/b”.

Is the following code is the best way to do this ?

//got $_SERVER['SCRIPT_NAME']  = /demo/index.php

    echo 'http://localhost' . $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'] ;

Im getting the result, but is there any direct way to access rewritten url ?

No I don’t of any faster way. I’d use the same way myself.

You could try


to see if there is anything of you’re liking, but I don’t think there is.