Search code examples
regex.htaccessmod-rewritelighttpd

Regex help: data after last slash and before interrogation and after interrogation


I have the following url´s types in my application:

http://192.168.0.191/myapp/public/controller_name/action_name
http://192.168.0.191/myapp/public/controller_name/action_name/param1/param2
http://192.168.0.191/myapp/public/controller_name/action_name?parameterlist

Ans I´m using the following REGEX for redirection:

I have the following Regex for redirection (everything goes through the index page):

"^([a-zA-Z0-9\/\-_]+)\.?([a-zA-Z]+)?$" redirects to "/myapp/public/index.php?url=$1&extension=$2")

Here are examples of URLs used:

http://192.168.0.191/myapp/public/customers -> Call customer controller
http://192.168.0.191/myapp/public/customers/detail/2 -> Show customer detail for id = 2

Both works fine. My problem is with the URL below - to load customer data for a Datagrid object:

http://192.168.0.191/myapp/public/customer/loadDtData?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=desc&start=0&length=25&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1450111563514

How can I fix the Regex to separate the text after the last / but before the ? and then text after the ? to pass as parameter to the application.

Thanks for helping. I´m using lighttpd as web server.

OBS: For Apache I would use the RewriteRule below to make the trick, but lighttpd does not have support for QSA:

RewriteRule ^([a-zA-Z0-9\/\-_]+)\.?([a-zA-Z]+)?$ index.php?url=$1&extension=$2 [QSA,L]

Solution

  • In absence of QSA flag try this rule:

    url.rewrite = ( "^/([\w/-]+)\.?([a-zA-Z]+)?(?:\?(.*))?" => "/index.php?url=$1&extension=$2&$3" )
    

    Reference