this is htaccess file on the server
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain.com(.*)$
RewriteRule ^(.*)$ http://domain.com/test\.php?user=%1&path=%2 [R]
According to my understanding of the above code if i request asher.domain.com/user it should rewrite to http://domain.com/test.php?user=asher&path=/user right?
Instead i get http://domain.com/test.php?user=asher&path= The %2 is empty. but if i use $1 instead of %2 i seem to get the right result.
I might be doing the silliest mistake ever but I am not sure where I my mistake is. Help me out here guys? where is the mistake in the rewrite rules that %2 is not working for me?
Using the
$syntax gives youRewriteRulebackreferences, while the%will give youRewriteCondbackreferences. Themod_rewritedocumentation covers this.In your case, your
RewriteRuleshould look like:Because you want the first group match from the previous
RewriteCondand the first group match from the currentRewriteRule.