mod_rewrite rules for FCGI-module

Alles, was den Apache betrifft, kann hier besprochen werden.

mod_rewrite rules for FCGI-module

Postby tsschulz » 23. April 2021 13:23

I try to get a rule(set) for forwading a request to an FastCGI-Module. The CGI is working, but I don't get the rule doing what I search for. There are some rules I need:

  • All requests (just not some folders) for that domain must be forwarded to the cgi-module (domain.url/ -> /cgi-bin/script.fcgi)
  • Pathes have to be appended to the script (domain.url/path/to -> /cgi-bin/script.fcgi/path/to)
  • Queries have also to be appended (domain.url?a=1 -> /cgi-bin/script.fcgi?a=1, domain.url/path/to?b=2 -> /cgi-bin/script.fcgi/path/to?b=2)
  • Some subfolders have to be forwarded to another subfolder (domain.url/sub -> /ext/sub)

I tried with that:
Code: Select all
RewriteEngine On
RewriteRule ^/sub/(.*)$ /ext/sub/$1 [L]
RewriteRule ^(.*)?$ /cgi-bin/script.fcgi/$1?%{QUERY_STRING} [L,QSA]


But unfortunately it isn't doing what I expect. The root is working, also the subfolder. But forwarding of a path and query string isn't working.
tsschulz
 
Posts: 2
Joined: 23. April 2021 13:19
XAMPP version: 7.4.16
Operating System: Linux - Tumbleweed

Re: mod_rewrite rules for FCGI-module

Postby Nobbie » 24. April 2021 12:58

You cannot access the QUERY_STRING in a RewriteRule, the query string is appended automatically to the new URL. As well the query string is NOT part of the parsed input URL (the first parameter of RewriteRule), also the question mark '?' is a metacharacter in the regular expressions. Delete it. Also ommit the parameter QSA, that is only needed if you want to add own parameters to the given query string. That finally results in:

Replace

Code: Select all
RewriteRule ^(.*)?$ /cgi-bin/script.fcgi/$1?%{QUERY_STRING} [L,QSA]


with

Code: Select all
RewriteRule ^(.*)$ /cgi-bin/script.fcgi/$1 [L]


Depending on Apache setting, you MAY have the need to skip a leading slash '/' in the URL, in that case you should try this RewriteRule:

Code: Select all
RewriteRule ^/(.*)$ /cgi-bin/script.fcgi/$1 [L]


But first try the other one, it may happen that you see two slashes like /cgi/script.fcgi//path/to?b=2. In that case use the second RewriteRule (but even two slashes should work).

Here is a nice tutorial of how query string is handled by mod_rewrite, read it carefully: https://simonecarletti.com/blog/2009/01 ... ry-string/
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: mod_rewrite rules for FCGI-module

Postby tsschulz » 25. April 2021 08:00

Thank you very much. It was one version I tried before, but I thought I made an error because it wasn't working completely.
But I found out that the Ajax Framework I'm using is appending an own query-string, and also I need to change the path to an query string. So I tried
Code: Select all
RewriteRule ^(.*)$ /cgi-bin/script.fcgi/?_=$1 [L]

The ?_= is also an requirement by the framework and not my idea.
For the pages itself it works, but the ajax requests don't work. They look like that:
Code: Select all
/path/to?param=xxx

So I think my rule would try to do a
Code: Select all
/cgi-bin/script/?_=/path/to?param=xxx
but correct would be
Code: Select all
/cgi-bin/script/?_=/path/to&param=xxx

Any idea how to solve that (if possible)?

Ah, and I did read the nice tutorial. Thank you!
tsschulz
 
Posts: 2
Joined: 23. April 2021 13:19
XAMPP version: 7.4.16
Operating System: Linux - Tumbleweed

Re: mod_rewrite rules for FCGI-module

Postby Nobbie » 25. April 2021 11:10

Maybe QSA option will do it, but i dont know, try it. It should work, but i am unsure because of the underscore. And either give us more precise information, or instead of rewriting the URL in .htaccess, you could also use PHP to build and redirect to the new URL, simply create an index.php or so which does redirection to your needs, PHP is much more powerfull than mod_rewrite.

Code: Select all
RewriteRule ^(.*)$ /cgi-bin/script.fcgi/?_=$1 [L,QSA]
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04


Return to Apache

Who is online

Users browsing this forum: No registered users and 27 guests