Apache: How to redirect OPTIONS request with .htaccess?

5,261
RewriteCond %{REMOTE_HOST} ^::1$
RewriteRule  ^OPTIONS  http://www.google.com/  [L]

that's my best guess, i'm certain of the RewriteCond, but not quite with the RewriteRule

it will match on the REMOTE_HOST being ::1 and then rewrite a request for any URL starting with OPTIONS to www.google.com

Share:
5,261

Related videos on Youtube

Milan Babuškov
Author by

Milan Babuškov

Software developer, owner of a small ISV company, project manager of the open source FlameRobin project. Specialized in Linux, C++, PHP and Relational databases. You can read my softvare related blog at http://www.BackwardCompatible.net You can also buy my shareware software at http://www.GuacoSoft.com

Updated on September 17, 2022

Comments

  • Milan Babuškov
    Milan Babuškov over 1 year

    I have Apache 2.2.4 server with a lot of messages like this in the access_log:

    ::1 - - [15/May/2010:19:55:01 +0200] "OPTIONS * HTTP/1.0" 400 543
    ::1 - - [15/May/2010:20:22:17 +0200] "OPTIONS * HTTP/1.0" 400 543
    ::1 - - [15/May/2010:20:24:58 +0200] "OPTIONS * HTTP/1.0" 400 543
    ::1 - - [15/May/2010:20:25:55 +0200] "OPTIONS * HTTP/1.0" 400 543
    ::1 - - [15/May/2010:20:27:14 +0200] "OPTIONS * HTTP/1.0" 400 543
    

    These are the "internal dummy connections" as explained on this page:

    http://wiki.apache.org/httpd/InternalDummyConnection

    The page also hits my main problem: "In 2.2.6 and earlier, in certain configurations, these requests may hit a heavy-weight dynamic web page and cause unnecessary load on the server. You can avoid this by using mod_rewrite to respond with a redirect when accessed with that specific User-Agent or IP address."

    Well, obviously I cannot use UserAgent because I minimized the server signature, but I could use IP address. However, I don't have a clue what should the RewriteCond and RewriteRule look for IPv6 address ::1.

    The website where this runs is using CodeIgniter, so there is already the following .htaccess in place, I just need to add to it:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/system.*
    RewriteRule ^(.*)$ /index.php?/$1 [G]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    

    Any idea how to write this .htaccess rule?

    Solved: Adding another rule makes OPTIONS fall through current rules and be handled the same way as Apache is doing by default.

    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/system.*
    RewriteRule ^(.*)$ /index.php?/$1 [G]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REMOTE_HOST} !^::1$
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    

    I never access the website via localhost on IPv6, so this works great.

  • DevGambit
    DevGambit almost 14 years
    also perhaps RewriteCond %{REQUEST_URI} ^OPTIONS.* and RewriteRule ^(.*)$ http://google.com/ [L] but only educated guessing
  • Milan Babuškov
    Milan Babuškov almost 14 years
    Thanks, your rule gave me the idea how to solve it, so I'll mark your answer as accepted.
  • DevGambit
    DevGambit almost 14 years
    what did you end up needing to do? and glad i could sort of help :)
  • Milan Babuškov
    Milan Babuškov almost 14 years
    I updated my question with the solution.
  • MrWhite
    MrWhite about 7 years
    Old question, but hey... From the access log in the question, OPTIONS isn't part of the URL-path. "OPTIONS" is the request method (as opposed to GET or POST). With mod_rewrite you can check for this against the REQUEST_METHOD server variable in a RewriteCond directive.