Title: Adding custom code to .htaccess
Last modified: December 19, 2023

---

# Adding custom code to .htaccess

 *  Resolved [Ben](https://wordpress.org/support/users/benleow/)
 * (@benleow)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/adding-custom-code-to-htaccess/)
 * I would like to add custom code to the .htaccess file which is located in the
   target directory. Each time I publish the site, the modified .htaccess got replaced(
   I have enabled “Generate .htaccess file providing actual HTTP redirects, HTTP
   status overrides, etc.”). I am wondering is it possible to add a new option in
   Staatic to include my custom code when I publish my site?
    -  This topic was modified 2 years, 5 months ago by [Ben](https://wordpress.org/support/users/benleow/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Team Staatic](https://wordpress.org/support/users/staatic/)
 * (@staatic)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/adding-custom-code-to-htaccess/#post-17287862)
 * Hello Ben,
 * Thank you for reaching out. The current version of Staatic makes it a bit tricky
   to add custom code to the `.htaccess` file. However, we’ve introduced new features
   to help with this. We’ve added two filter hooks: `staatic_filesystem_apache_config`
   and `staatic_filesystem_nginx_config`. These allow you to modify the `.htaccess`
   and `nginx_rules.conf` files before saving them.
 * To access these new features, you need to update to the latest development version
   of Staatic. Here’s how to do it:
    1. Go to the WordPress Plugin Directory.
    2. Find Staatic, click on **Advanced View**.
    3. Scroll down to **Advanced Options**.
    4. Choose **Development Version** and click **Download**.
    5. Install this new version.
 * For guidance on using these hooks, check out this example: [staatic_filesystem_apache_config.php](https://gist.github.com/staaticdotcom/56aba9b8145fba2e5cefe8284d5ecfe4).
   You can add this code to your theme’s `functions.php` file, or as a new file 
   in `wp-content/mu-plugins` (like `staatic_filesystem_apache_config.php`).
 * If you have any more questions or need help, feel free to ask.
 *  Thread Starter [Ben](https://wordpress.org/support/users/benleow/)
 * (@benleow)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-code-to-htaccess/#post-17318513)
 * Thanks! It works as expected. It would be great if there is textarea for user
   to input via GUI instead of via functions.php
 *  Thread Starter [Ben](https://wordpress.org/support/users/benleow/)
 * (@benleow)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-code-to-htaccess/#post-17319407)
 * Alternatively, maybe you can provide these options in the GUI:
    1. Redirect non-www URL to www URL and vice versa
    2. Compress components with gzip
    3. Add Expires headers
 *  Plugin Author [Team Staatic](https://wordpress.org/support/users/staatic/)
 * (@staatic)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-code-to-htaccess/#post-17319555)
 * Thank you for your feedback and suggestions. We are glad to hear that the new
   filter hooks have been working well for you.
 * Your suggestion for a GUI textarea for custom code is noted. It’s an interesting
   idea that could simplify the user experience. We’re also considering your other
   suggestions, like options for URL redirections and gzip compression.
 * If you have any other ideas or feedback, please feel free to share them with 
   us.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Adding custom code to .htaccess’ is closed to new replies.

 * ![](https://ps.w.org/staatic/assets/icon.svg?rev=2550278)
 * [Staatic - Static Site Generator for WordPress](https://wordpress.org/plugins/staatic/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/staatic/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/staatic/)
 * [Active Topics](https://wordpress.org/support/plugin/staatic/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/staatic/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/staatic/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Team Staatic](https://wordpress.org/support/users/staatic/)
 * Last activity: [2 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-code-to-htaccess/#post-17319555)
 * Status: resolved