能否通过mod-rewrite修改REQUEST_METHOD?(HTTPD 2.4.6)
Can I Change POST to PUT with Apache 2.4.6 Rewrite Rules?
Absolutely, this is totally feasible in Apache HTTPD 2.4.6—let’s fix the issues in your current setup and get this working properly.
First, let’s address a common typo that might have been tripping you up: Apache uses %{REQUEST_URI} (all caps) for the request path variable, not %{Request_url}. That’s probably one reason your rules weren’t matching as expected.
Here’s the corrected configuration you need:
RewriteEngine On # Match the target URL path, POST method, and custom header RewriteCond %{REQUEST_URI} ^/sometext/ RewriteCond %{REQUEST_METHOD} POST RewriteCond %{HTTP:myheader} =true # Override the request method to PUT RewriteRule ^ - [E=REQUEST_METHOD:PUT]
Let’s break down what each part does:
RewriteEngine On: Enables the mod_rewrite module—this is non-negotiable for any rewrite rules to run.- First
RewriteCond: Targets requests where the URI starts with/sometext/(adjust the path if your actual target is different). - Second
RewriteCond: Filters for only POST requests. - Third
RewriteCond: Checks that the custommyheaderis exactly equal totrue(the=ensures an exact match; omit it if you need partial matching). RewriteRule ^ - [E=REQUEST_METHOD:PUT]: The^ -means we don’t modify the request URL at all. TheEflag sets theREQUEST_METHODenvironment variable toPUT, which Apache will use for the rest of the request processing.
Quick checks to ensure this works:
- Verify mod_rewrite is loaded: Run
httpd -Min your terminal and look forrewrite_module (shared)in the output. If it’s missing, enable it in your Apache config (usually withLoadModule rewrite_module modules/mod_rewrite.so). - .htaccess permissions: If you’re using an .htaccess file, make sure the parent directory has
AllowOverride FileInfoorAllowOverride Allset in your main Apache config—otherwise, the rewrite rules will be ignored. - Test with curl: Validate the setup using this command (replace
yourdomain.comwith your actual server):
Check the server response logs—you should see the request method logged as PUT instead of POST.curl -X POST -H "myheader: true" http://yourdomain.com/sometext/test -v
This approach works reliably in Apache 2.4.6 because the REQUEST_METHOD environment variable is mutable via mod_rewrite’s E flag, and Apache respects this modified value during request handling.
内容的提问来源于stack exchange,提问作者aditya
相关产品推荐
相关产品推荐

