mod_rewrite only on GET

2020-08-23 02:30发布

问题:

It's a longshot, but I'm hoping to find a simple workaround for a bizarre bug that only manifests when the query string is omitted/inferred by the application.

Before I dig deep into a thousand lines of minified 3rd party javascript, I'd like to find out if I can just auto apply the querystring using mod_rewrite.

RewriteRule    ^index\.php$  index.php?module=Home&action=index

Now, this would work fine except sometimes all the data will be POSTed so I need a RewriteCond so the rule will only fire on GET requests, and not POST requests.

Is this possible?

回答1:

Add this condition...

RewriteCond %{REQUEST_METHOD} !POST

...to not match POST requests.



回答2:

I'd recommend being explicit and only firing the RewriteRule when the request method is GET, rather than whenever it's not POST as there are numerous other methods. So your rewrite condition could look like this:

RewriteCond %{REQUEST_METHOD}  =GET

RewriteRule    ^index\.php$  index.php?module=Home&action=index


回答3:

From the docs:

  • Server-Variables: These are variables of the form %{ NAME_OF_VARIABLE } where NAME_OF_VARIABLE can be a string taken from the following list:

    ...

connection & request:
...
REQUEST_METHOD

So, yeah. Use RewriteCond with that server variable.



回答4:

This works fine for GET requests...

RewriteCond %{REQUEST_METHOD} ^GET [NC]