Search code examples
apache.htaccessmod-rewriteurl-rewriting

How to block post method on some url in htaccess


Is it possible to block POST method on some url like

www.mydomain.com/dontposthere
www.mydomain.com/something/againdontposthere

in htaccess ?


Solution

  • If you have access to mod_rewrite, you can check the REQUEST_METHOD environment variable and rewrite the url to another page that displays a message saying something like "You are not allowed to post"

    Create a page with the message: /var/www/noPost.php

    You are not allowed to post to this page
    

    Then use an .htaccess rule like this:

    RewriteEngine On
    
    RewriteCond %{REQUEST_METHOD} POST
    RewriteCond %{REQUEST_URI} ^/dontposthere [OR]
    RewriteCond %{REQUEST_URI} ^/something/againdontposthere
    RewriteRule .* /noPost.php [L,QSA]
    

    You can create a list of files/folders as conditions to match or even a regex pattern to match multiple files/folders. You would just need to put [OR] after all but the last.