Title: Cannot write .htaccess
Last modified: November 24, 2020

---

# Cannot write .htaccess

 *  [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/)
 * Hi,
    i try to make some changes on iThemes Security but it says “.htaccess cannot
   be written”. “Allow iThemes Security to write on wp-config.php et .htaccess” 
   is checked. .htaccess rights are 644. Any idea ?
 * thanks 🙂
    -  This topic was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).
    -  This topic was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).

Viewing 15 replies - 1 through 15 (of 37 total)

1 [2](https://wordpress.org/support/topic/cannot-write-htaccess/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/cannot-write-htaccess/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/cannot-write-htaccess/page/2/?output_format=md)

 *  [nlpro](https://wordpress.org/support/users/nlpro/)
 * (@nlpro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13702198)
 * Probably best to enable the **Show Error Codes** setting in the **Global Settings**
   module (Scroll down all the way to the bottom to find the setting).
 * Then reproduce the error and share all the error info displayed on your screen.
 * To prevent any confusion, I’m not iThemes.
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13702752)
 * Thanks. Here is the full error:
    /var/www/html/wordpress/.htaccess n’a pas pu
   être écrit. C’est peut être dû à un problème de permissions. Assurez-vous que
   PHP est lancé avec les permissions utilisateurs nécessaires pour écrire à cet
   emplacement. (itsec-lib-file-write-file-put-contents-failed)
 *  [nlpro](https://wordpress.org/support/users/nlpro/)
 * (@nlpro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13702934)
 * Ok, so the full error translated in English is:
 * > /var/www/html/wordpress/.htaccess could not be written. This could be due to
   > a permissions issue. Ensure that PHP runs as a user that has permission to 
   > write to this location.(itsec-lib-file-write-file-put-contents-failed)
 * You will have to contact your hosting provider and let them check that the user
   running the PHP process has write permission to your /var/www/html/wordpress/.
   htaccess file.
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13703244)
 * I host my linux server on OVH. Everything else is made by myself.
    www-data is
   the wordpress folder owner. If i put “echo(exec(“whoami”));die();” on index.php,
   www-data is shown as user running. .htaccess is 644.
    -  This reply was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).
 *  [nlpro](https://wordpress.org/support/users/nlpro/)
 * (@nlpro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13703408)
 * Ok, I see. You may need to debug the iTSec plugin script responsible for generating
   the error. It’s the **write** method in the wp-content/plugins/better-wp-security/
   core/lib/class-itsec-lib-file.php file.
 * The **write** method is called like this:
 * `return ITSEC_Lib_File::write( $file, $contents );`
 * Return values are boolean or WP_Error. Boolean true on success, WP_Error object
   otherwise.
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13706024)
 * I put the write method on the class-itsec-lib-file.php file and now it returns:
   `(
   itsec-settings-page-server-error-ajax-response: error "Internal Server Error")`
 *  [nlpro](https://wordpress.org/support/users/nlpro/)
 * (@nlpro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13706488)
 * Ah, ok. My apologies. I should explain better.
 * Whatever change(s) you made, undo it first. Then follow the steps below:
 * In the better-wp-security/core/lib folder create a backup copy of the original
   class-itsec-lib-file.php file. Name it class-itsec-lib-file.php.old
    We are making
   this copy to be able to quickly restore the default setup after we are done debugging.
 * Then create a new file (itsec-write-test.php) in the WordPress **wp-admin** folder
   and enter the following content in the file:
 *     ```
       <?php
   
       /** WordPress Administration Bootstrap */
       require_once __DIR__ . '/admin.php';
   
       /** Only users with manage options capability allowed */
       if ( ! current_user_can( 'manage_options' ) ) {
       	wp_die( __( 'Sorry, you are not allowed to manage options on this site.' ) );
       }
   
       /** Write to a new test file */
       $result = ITSEC_Lib_File::write( '/var/www/html/wordpress/test1.txt', "This is a test.\n" );
   
       /** Check and show the result */
       if ( is_wp_error( $result ) ) {
       	 echo 'Error msg: ' . $result::get_error_message();
       } else {
       	 echo 'File writing ' . ( $result?'succeeded.':'failed.' );
       }
       ```
   
 * Next (from the browser) you should log into the WordPress Dashboard as usual.
   Then open a second tab in the browser and enter the address to the itsec-write-
   test.php file:
 * > [http://www.domain.fr/wp-admin/itsec-write-test.php](http://www.domain.fr/wp-admin/itsec-write-test.php)
 * Where [http://www.domain.fr](http://www.domain.fr) should be substituted with
   your domain name. (If using SSL use https instead of http).
 * At this point, let me know the first and second result (simply refresh the second
   tab in the browser for the second result).
 * We will continue debugging after these results are known.
    -  This reply was modified 5 years, 5 months ago by [nlpro](https://wordpress.org/support/users/nlpro/).
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13707158)
 * Done (you missed ?> at the end of code) 🙂
    Result => File writing succeeded.
 *  [nlpro](https://wordpress.org/support/users/nlpro/)
 * (@nlpro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13707673)
 * Ok, great.
 * The test script is based upon the wp-admin/index.php file. There is no php end
   tag included in that file so I saw no reason to add it to our test script 😉 
   Also most WordPress php files do not include the php end tag. It’s not a fatal
   error …
 * Anyway, we have a positive result when writing to a NEW file in the same folder
   where the .htaccess file exists. Did the second write test also return a positive
   result ? (Oh, to keeps things tidy you can delete the newly created test1.txt
   file).
 * Before starting debugging we need to make sure we can reproduce the issue when
   using the test script. So next we will try to write to the existing .htaccess
   file.
 * **IMPORTANT**: First create a backup copy of the .htaccess file !!
 * Then change the test script (itsec-write-test.php) and simply change the filename“
   test1.txt” into “.htaccess”
 * Now run the test script again. If the issue reproduces it should return:
 * > Error msg: /var/www/html/wordpress/.htaccess could not be written. This could
   > be due to a permissions issue. Ensure that PHP runs as a user that has permission
   > to write to this location.
 * If the issue does not reproduce, check the content of the .htaccess file. If 
   the content is overwritten RESTORE the .htaccess file from the backup file we
   created earlier!
    -  This reply was modified 5 years, 5 months ago by [nlpro](https://wordpress.org/support/users/nlpro/).
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13708022)
 * i delete test1.txt. But i dont understand how to perform the “second write test”?
   
   Do you mean this: “Then change the test script (itsec-write-test.php) and simply
   change the filename “test1.txt” into “.htaccess” Now run the test script again.”?
    -  This reply was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13708093)
 * Done: change the itsec-write-test.php to
 *     ```
       /** Write to a new test file */
       $result = ITSEC_Lib_File::write( '/var/www/html/wordpress/.htaccess', "This is a .htaccess test.\n" );
       ```
   
 * And i have this error:
 *     ```
       une erreur critique est survenue sur votre site. Veuillez consulter la boite de réception de l’e-mail d’administration de votre site pour plus d’informations.
       En apprendre plus sur le débogage de WordPress.
       ```
   
 * .htaccess is not modified and WP is up.
 * I use Wordfence. It’s not a problem ?
    -  This reply was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).
    -  This reply was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).
 *  [nlpro](https://wordpress.org/support/users/nlpro/)
 * (@nlpro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13708216)
 * Using Wordfence should not be a problem.
 * I think adding .htaccess twice may be the problem.
 * So change the write line to look like this:
 * `$result = ITSEC_Lib_File::write( '/var/www/html/wordpress/.htaccess', "This 
   is a test.\n" );`
 * Then retry.
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13708271)
 * Same error.
    itsec-write-test.php content:
 *     ```
       <?php
   
       /** WordPress Administration Bootstrap */
       require_once __DIR__ . '/admin.php';
   
       /** Only users with manage options capability allowed */
       if ( ! current_user_can( 'manage_options' ) ) {
               wp_die( __( 'Sorry, you are not allowed to manage options on this site.' ) );
       }
   
       /** Write to a new test file */
       /**$result = ITSEC_Lib_File::write( '/var/www/html/wordpress/.htaccess', "This is a .htaccess test.\n" );*/
       $result = ITSEC_Lib_File::write( '/var/www/html/wordpress/.htaccess', "This is a test.\n" );
   
       /** Check and show the result */
       if ( is_wp_error( $result ) ) {
                echo 'Error msg: ' . $result::get_error_message();
       } else {
                echo 'File writing ' . ( $result?'succeeded.':'failed.' );
       }
       ```
   
    -  This reply was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).
 *  [nlpro](https://wordpress.org/support/users/nlpro/)
 * (@nlpro)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13708385)
 * Last option, remove the extra comment line:
 * `/**$result = ITSEC_Lib_File::write( '/var/www/html/wordpress/.htaccess', "This
   is a .htaccess test.\n" );*/`
 *  Thread Starter [gregmurf](https://wordpress.org/support/users/gregmurf/)
 * (@gregmurf)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/#post-13708394)
 * Still the same error:
    [WP error](https://www.google.com/imgres?imgurl=https%3A%2F%2Fwww.wpfixit.com%2Fwp-content%2Fuploads%2F2019%2F11%2FThere-has-been-a-critical-error-on-your-website.-Please-check-your-site-admin-email-inbox-for-instructions..jpg&imgrefurl=https%3A%2F%2Fwww.wpfixit.com%2Fwordpress-error-there-has-been-a-critical-error-on-your-website%2F&tbnid=dV2w4Fb_aCL6YM&vet=12ahUKEwiCkKep_53tAhUX_hoKHbCRC3gQMygAegUIARCRAQ..i&docid=oV3964jq7g07UM&w=826&h=209&q=a%20critical%20error%20has%20occurred%20on%20your%20site.%20wordpress&ved=2ahUKEwiCkKep_53tAhUX_hoKHbCRC3gQMygAegUIARCRAQ)
    -  This reply was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).
    -  This reply was modified 5 years, 5 months ago by [gregmurf](https://wordpress.org/support/users/gregmurf/).

Viewing 15 replies - 1 through 15 (of 37 total)

1 [2](https://wordpress.org/support/topic/cannot-write-htaccess/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/cannot-write-htaccess/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/cannot-write-htaccess/page/2/?output_format=md)

The topic ‘Cannot write .htaccess’ is closed to new replies.

 * ![](https://ps.w.org/better-wp-security/assets/icon.svg?rev=2980272)
 * [Solid Security – Password, Two Factor Authentication, and Brute Force Protection](https://wordpress.org/plugins/better-wp-security/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/better-wp-security/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/better-wp-security/)
 * [Active Topics](https://wordpress.org/support/plugin/better-wp-security/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/better-wp-security/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/better-wp-security/reviews/)

 * 37 replies
 * 2 participants
 * Last reply from: [nlpro](https://wordpress.org/support/users/nlpro/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/cannot-write-htaccess/page/3/#post-13713294)
 * Status: not resolved