mod_rewrite only on GET

雨燕双飞 提交于 2020-06-11 17:58:12

问题


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]


来源:https://stackoverflow.com/questions/8222815/mod-rewrite-only-on-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!