Custom .htaccess rules

Custom .htaccess rules

Sometimes you just need to add custom .htaccess rules beyond what the .htaccess Maker can offer. Such examples can be special directives your host told you to include in your .htaccess file to enable a different version of PHP, change the server's default error documents and so on. If you are an advanced user you may also want to write your own advanced rules to further customize the behaviour of the Server Protection. The two options in this section allow you to do that.

The contents of the Custom .htaccess rules at the top of the file text area will be output at the top of the file, just after the RewriteEngine On directive. You should put custom exception rules and, generally, anything which should run before the protection and security rules in here.

The contents of the Custom .htaccess rules at the bottom of the file text area are appended to the end of the .htaccess file. This is the place to put stuff like directives to enable a different PHP version and any optimizations which should run only after the request has passed through the security and server protection rules.

Changing the PHP version and the .htaccess Maker

Every time you change the PHP version of your site in your hosting control panel, the host's control panel adds or modifies some lines towards the very bottom of your .htaccess file. These are lines using the Apache configuration directives AddHandler or SetHandler.

For example, on a cPanel host you will see the following (the “broken” characters are what cPanel outputs, they are not a typo in our documentation):

# php -- BEGIN cPanel-generated handler, do not edit
# Set the <E2><80><9C>ea-php80<E2><80><9D> package as the default <E2><80><9C>PHP<E2><80><9D> programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php80 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

On another host we see just a bare AddHandler line:

AddHandler application/php80 .php

On a self-managed Apache server we may instead see something along the following lines (in this case the FileMatch lines are important and must be copied too; they tell Apache to only run files whose name ends in .php through the specific PHP FastCGI Process Manager proxy):

<FilesMatch "\.php$">
  SetHandler "proxy:fcgi://127.0.0.1:9081/"
</FilesMatch>

When you regenerate the .htaccess file using the Save & Create .htaccess these lines will be overwritten by the .htaccess Maker as it replaces your .htaccess file with the new one.

To prevent that from happening you need to copy these special lines which set the PHP version into the Custom .htaccess rules at the bottom of the file setting in the .htaccess Maker. By doing so, these lines will indeed appear in the generated .htaccess file.

Remember to change these lines in your .htaccess Maker configuration every time you upgrade (or downgrade) the PHP version on your site.

[Tip]Tip

.htaccess Maker will check whether your .htaccess file has any lines which appear to be setting the PHP version. If these lines are not already transcribed into Custom .htaccess rules at the bottom of the file, or if they differ to what is transcribed, it will offer to transcribe these lines for you.