Maj. Fail
Maj. Fail

Reputation: 67

Problem with htaccess GET form variables in a rewritten url

Essentially my problem is thus; I have a MVC system that redirects all requests to index.php on my site. I have a rewrite rule in my htaccess file to handle those requests like so:

RewriteRule ^([a-zAZ\_\-]+)\/([a-zA-Z\_\-]+)\/([^\/?]*) /?module=$1&class=$2&event=$3

Which translates urls into these type of urls

http://example.com/users/login/
http://example.com/users/info/me

My problem is that I also want GET variables to be applied and used in the URL like so

http://example.com/users/login/?var1=val1&var2=val2
http://example.com/users/info/me?var1=val2...

I've written two different regexes that work perfectly well in a my workbench (expresso) and I've tested them out in PHP however they refuse to work in htaccess. They're not particular complex, I have tried:

^([a-zAZ_\-]+)\/([a-zA-Z_\-]+)\/([^\/\?]*)[\?]*(.*) /?module=$1&class=$2&event=$3&$4
and
^([a-zAZ_\-]+)\/([a-zA-Z_\-]+)\/([^\/\?]*)(?(?=\?)\?(.+)) /?module=$1&class=$2&event=$3&$4

Neither of these work and I'm racking my brains as to why. Essentially it just doesn't recognise the fourth group and returns nothing I thought it might have been due to it being next to an ampersand but I did &var=$4 as a test and it still fell over.

Any help with this would be greatly appreciated as this is driving me insane.

Thanks in advance, Rupert S.

Upvotes: 0

Views: 1588

Answers (1)

aorcsik
aorcsik

Reputation: 15552

After all, this is what you need:

RewriteRule ^([a-z_-]+)/([a-z_-]+)/([^/?]*) /?module=$1&class=$2&event=$3 [QSA,NC,L]

[QSA] will append the additional GET parameters to the rewritten query string.

[NC] since it is case insensitive, no need for A-Z matches

Upvotes: 3

Related Questions