user1514176
user1514176

Reputation: 1

.htaccess RewriteRule backreference order

I have a question about htaccess rewriting rule.

Is it possible to convert this url:

www.site.com/en/test

In something like this:

www.site.com/test.php?language=en

I tried to use this RewriteRule

RewriteRule ^([a-z][a-z])/(.*)$ $1.php?language=$2

But in some online testing tool the result URL was:

www.site.com/en.php?language=test

Thank you in advance. Mat

Upvotes: 0

Views: 1145

Answers (1)

Jenny D
Jenny D

Reputation: 1245

The reason for this confusion is that you've reversed the matches. When you use references, the first reference in the match is always $1, the second is $2 and so on.

Here's what you did, looking only at the parentheses:

([a-z][a-z]) = first match, places "en" in $1 
(.*) = second match, places "test" in $2

So you simply need to reverse them in your output, like this:

RewriteRule ^([a-z][a-z])/(.*)$ $2.php?language=$1

Upvotes: 1

Related Questions