How can I disallow Python-requests and return the 418 code

I want to make a anti-spider joke.

Here is the answer from AI (Copilot)

The 418 code is a humorous HTTP status code that means “I’m a teapot” and is not meant to be used seriously. However, if you want to return it for some reason, you can do so by using the following steps:

  • In your .htaccess file, add a rewrite rule that checks the user-agent header for python-requests and redirects it to a PHP script that returns the 418 code. For example:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} python-requests
RewriteRule ^.*$ teapot.php [L]
  • In your teapot.php file, use the header function to set the status code to 418 and the message to “I’m a teapot”. You can also output some HTML content if you want. For example:
<?php
header ('HTTP/1.1 418 I\'m a teapot');
echo '<h1>I\'m a teapot</h1>';
echo '<p>This server refuses to brew coffee because it is, permanently, a teapot.</p>';
?>
  • Save the files and test your server with python-requests. You should get a response like this:
<Response [418]>
6 Likes

It doesn’t work! There’s no any response. But I got a connection error. When I change my user-agent, I got a page about enable JavaScript. If I delete the .htaccess file, it’s the same as above.

10 Likes

OK. All in all, I know that.

I did not test the AI code

but if you get a 403 then that’s because you don’t have an index file.
In short, play with it all (rename teapot to index and similar) or look for better code online

And if there are still problems, maybe the hosting itself is blocking that UA (treats as bad UA)

Also I don’t know if the values for UA are case-sensitive in .htaccess or not

and if necessary you can add [NC] flag

NC = flag ignores the case

RewriteCond %{HTTP_USER_AGENT} python-requests [NC]

if you have a space in the UA name, then put the value between quotes

RewriteCond %{HTTP_USER_AGENT} "python some space UA requests" [NC]
6 Likes

here are a few to make it clearer


# Forces the server to ignore the text following the # on the same line. Typically used for comments
[F] Indicates Forbidden, with this the server should return a 403 forbidden error to the client
[L] The Last rule forces the server to stop processing rules in the .htaccess file
[N] Indicates Next and forces Apache to redo the rewrite process, except using the currently rewritten URL instead of the initial URL
[G] Gone tells the server to deliver the gone status message, which is used to mark pages that no longer exist on the site.
[R] This forces Apache to initialize a redirect, this can be a permanent redirect (page has moved, 301), or a temporary redirect (302).
[P] Indicates Proxy which tells the server to use mod_proxy to handle requests
[C] Tells the server to chain a rule with the next rule. If the rule matches for example, then the chained rules will run as well, if not, then they will not run.
[QSA] Tells the server to use the query string at the end of an expression
[NC] No Case instructs the server to treat any argument as case insensitive
[NS] The No Subrequest forces the server to skip if it is an internal subrequest
[PT] Pass-Through has mod_rewrite send a formatted URL back to Apache
[NE] No Escape forces the server to parse through all output ignoring escaping characters, meaning spaces in the URL will not be replaced with %20 for example
[OR] Specifies a logical ‘OR‘ statement that evaluates two expressions
[S=x] Forces the server to skip “x” number of rules based on if a match is found, not the same as the Chain flag [C]
[a-z] Denotes a range of characters between the two characters separated by a dash
[^] Defines not within a character class, or the Start of a string of characters
[]+ Defines that any combination of characters defined within the brackets is a match there can be multiple matches
[] Defines that any characters defined within the brackets are a match
[T=MIME-type] Defines the mime type, forces the target file to be that mime type
[E=variableName:newValue] Forces the server to set the environmental variable “variableName” to the value “newValue”
a{n} Defines the specific number of the preceding character to be matched
? Defines the preceding character as being optional
$ Signals the end of a regular expression
() Can be used to group characters together
^ Signals the beginning of a regular expression
. Specifies a single arbitrary character
Signals not to perform an action
! Defines negation
+ Will match at least one preceding character
| Logical ‘OR‘ operator
* Wildcard that will match any occurrence of the preceding character
. Signals an escaped literal period
-d Analyzes if a string exists within a directory
-f Determines if a string is a preexisting file
-s Tests for a non zero value

CO=cookie (set a specified cookie)

9 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.