Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Apache mod_rewrite rewritecond with POST parameters?

I know I can inspect GET query string parameters in rewritecond as follows:

RewriteCond %{REQUEST_URI} ^/somepath/somepath
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} try=3
RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule ^/somepath/somepath(.*) /otherpath/otherpath?name=%1 [R]

How do I inspect POST parameters that are in the request body? I hear mod_security can do it, but I'm not finding any examples of how I'd use mod_security in conjunction with mod_rewrite like the above example.

I intend to use something like this to handle POSTs:

RewriteCond %{REQUEST_URI} ^/somepath/somepath
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^/somepath/somepath(.*) /otherpath/otherpath [PT]

...except that I need a RewriteCond that inspects the POST parameters to see if "try=3".

Can modsecurity inspect the request body and load the result of that inspection in an environment variable? that would work...

like image 573
Jaffadog Avatar asked Dec 03 '13 19:12

Jaffadog


People also ask

How do I enable mod rewrite?

Step 1 — Enabling mod_rewrite In order for Apache to understand rewrite rules, we first need to activate mod_rewrite . It's already installed, but it's disabled on a default Apache installation. Use the a2enmod command to enable the module: sudo a2enmod rewrite.

How do you write rewrite rules in httpd conf?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

What is RewriteCond in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


1 Answers

You can't inspect the request body using mod_rewrite.

You may have to rewrite POST requests to a script if that's something that you can do. Browser's aren't always going to resend POST data if you redirect them.

like image 66
Jon Lin Avatar answered Oct 29 '22 17:10

Jon Lin