Fixing wordpress WP ROCKET messed up rewriten .htaccess

Started on the old forum in October 2015 · view the original in the archive · old links redirect here

1 reply · 1,014 views · started 11 years ago · 4 min read

2 participants
just_m3. Developer · 199 posts #1

I have recently had some problems with a wordpress website on which i'm working , i have installed wp-rocket and recently found my site "dead" with loop issue , i was like "what the hell happent" ?

Enabled debug from config no error .. hmm thinking , must be server side , lets check .htaccess , well yeah .. that was the issue , messed up rewrite lines , incomplete lines and so on , that's because the "dumb" developers of the plugin chosed to write their own "write to htaccess rulles" function , wordpress already has that function built in so you can add rulles with markers .

Here are the modified functions from wp-rocket htaccess.php file that writes rulles on htaccess , you can find it in :

/wp-content/plugins/wp-rocket/inc/front/htaccess.php

function flush_rocket_htaccess( $force = false )
{
    if ( ! $GLOBALS['is_apache'] ) {
        return;
    }
    
    global $wp_rewrite;

    $rules = '';

    $home_path = get_home_path();
    
    $htaccess_file = $home_path.'.htaccess';
 
    /*
     * If the file doesn't already exist check for write access to the directory
     * and whether we have some rules. Else check for write access to the file.
     */
    if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
    
        if ( got_mod_rewrite() ) {
        
        if ( $force === false ) {
            $rules = get_rocket_htaccess_marker();
        }
        
        return insert_with_markers( $htaccess_file, 'WP Rocket', $rules );

        }
    }
    
        // Update the .htacces file
        //rocket_put_content( $htaccess_file, $rules . $ftmp );
}

/**
 * Return the markers for htacces rules
 *
 * @since 1.0
 *
 * @return string $marker Rules that will be printed
 */
function get_rocket_htaccess_marker()
{

        $marker  = get_rocket_htaccess_charset();
        $marker .= get_rocket_htaccess_etag();
        $marker .= get_rocket_htaccess_web_fonts_access();
        $marker .= get_rocket_htaccess_files_match();
        $marker .= get_rocket_htaccess_mod_expires();
        $marker .= get_rocket_htaccess_mod_deflate();
        
        /** This filter is documented in inc/front/process.php */
        
        if ( apply_filters( 'do_rocket_generate_caching_files', true ) ) {
        $marker .= get_rocket_htaccess_mod_rewrite();    
        }
    
        
        $rules = explode( "\n", $marker );

            
    /**
     * Filter rules added by WP Rocket in .htaccess
     *
     * @since 2.1
     *
     * [USER="20491"]param[/USER] string $marker The content of all rules
    */
    
    $rules = apply_filters('rocket_htaccess_marker' , $rules );

    return $rules;
}

enjoy and have a nice day , i'm out !

It seems that they need to add their htaccess lines before others so the compression could work as it should, here are the modified functions :

/* for testing purposes , create a htaccess file with the following 

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# BEGIN otherewritedemo
# END otherewritedemo

*/
    
    
    

  /* bellow you have the modified + added functions */

 
  /*added modified native worpdress insert htaccess function */
  
 function wprocket_insert_with_markers( $filename, $marker, $insertion ) {
    if (!file_exists( $filename ) || is_writeable( $filename ) ) {
        if (!file_exists( $filename ) ) {
            $markerdata = '';
        } else {
            $markerdata = explode( "\n", implode( '', file( $filename ) ) );
        }

        if ( !$f = @fopen( $filename, 'w' ) )
            return false;

        $foundit = false;
        if ( $markerdata ) {
            $state = true;
            $add_at_bottom="";
            
            foreach ( $markerdata as $n => $markerline ) {
                if (strpos($markerline, '# BEGIN ' . $marker) !== false)
                    $state = false;
                if ( $state ) {
                    /* build already existing lines list */
                    if ( $n + 1 < count( $markerdata ) )
                        $add_at_bottom .="{$markerline}\n";
                        
                    else
                        $add_at_bottom .="{$markerline}";
                }
                if (strpos($markerline, '# END ' . $marker) !== false) {
                    fwrite( $f, "# BEGIN {$marker}\n" );
                    if ( is_array( $insertion ))
                        foreach ( $insertion as $insertline )
                            fwrite( $f, "{$insertline}\n" );
                    fwrite( $f, "# END {$marker}\n" );
                    $state = true;
                    $foundit = true;
                }
            }
            
        }
        if (!$foundit) {
            fwrite( $f, "\n# BEGIN {$marker}\n" );
            foreach ( $insertion as $insertline )
                fwrite( $f, "{$insertline}\n" );
            fwrite( $f, "# END {$marker}\n" );
        }
        
        /* add them at the bottom */
        @fwrite( $f, $add_at_bottom );
            
        fclose( $f );
        return true;
    } else {
        return false;
    }
}
 
 
function flush_rocket_htaccess( $force = false )
{
    if ( ! $GLOBALS['is_apache'] ) {
        return;
    }
    
    global $wp_rewrite;
    
    $rules = '';

    $home_path = get_home_path();
    
    $htaccess_file = $home_path.'.htaccess';
 
    /*
     * If the file doesn't already exist check for write access to the directory
     * and whether we have some rules. Else check for write access to the file.
     */
    if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
    
        if ( got_mod_rewrite() ) {
        
        if ( $force === false ) {
            $rules = get_rocket_htaccess_marker();
        }
        
        $return = wprocket_insert_with_markers( $htaccess_file, 'WP Rocket', $rules );
        
        
        return $return;

        }
    }
    
}

/**
 * Return the markers for htacces rules
 *
 * @since 1.0
 *
 * @return string $marker Rules that will be printed
 */
 

function get_rocket_htaccess_marker()
{

        $marker  = get_rocket_htaccess_charset();
        $marker .= get_rocket_htaccess_etag();
        $marker .= get_rocket_htaccess_web_fonts_access();
        $marker .= get_rocket_htaccess_files_match();
        $marker .= get_rocket_htaccess_mod_expires();
        $marker .= get_rocket_htaccess_mod_deflate();
        
        /** This filter is documented in inc/front/process.php */
        
        if ( apply_filters( 'do_rocket_generate_caching_files', true ) ) {
        $marker .= get_rocket_htaccess_mod_rewrite();    
        }

        
        $rules = explode( "\n", $marker );

            
    /**
     * Filter rules added by WP Rocket in .htaccess
     * 
     * @since 2.1
     *
     * [USER="20491"]param[/USER] string $marker The content of all rules
    */
    
    $rules = apply_filters('rocket_htaccess_marker' , $rules );

    return $rules;
}

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

arnage Lead · 811 posts #2

Its a big script so no wonder why they wrote their own... Nice catch mate.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close