Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
Woocommerce-multilingual plugin rewrites htaccess and makes site crash.

Solution:
The problem actually comes from certain third-party plugins calling flush_rewrite_rules(true) function too often. The proper fix is for the third-party plugin to flush the rewrite rules only once.

However, we can also avoid the language folder from being added within WPML. As a temporary workaround you can add the following lines to the functions.php of your theme:

add_filter('mod_rewrite_rules', 'fix_rewritebase');
function fix_rewritebase($rules){
    $home_root = parse_url(home_url());
    if ( isset( $home_root['path'] ) ) {
        $home_root = trailingslashit($home_root['path']);
    } else {
        $home_root = '/';
    }
  
    $wpml_root = parse_url(get_option('home'));
    if ( isset( $wpml_root['path'] ) ) {
        $wpml_root = trailingslashit($wpml_root['path']);
    } else {
        $wpml_root = '/';
    }
  
    $rules = str_replace("RewriteBase $home_root", "RewriteBase $wpml_root", $rules);
    $rules = str_replace("RewriteRule . $home_root", "RewriteRule . $wpml_root", $rules);
  
    return $rules;
}

Relevant Documentation:
- https://wpml.org/errata/htaccess-is-rewritten-with-language-folder/

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 2 replies, has 2 voices.

Last updated by roxanneS 6 months, 1 week ago.

Assigned support staff: Dražen Duvnjak.

Author Posts
January 13, 2021 at 8:38 pm #7838935

roxanneS

I am trying to: Access any page beyond the home page.

Link to a page where the issue can be seen:Any page but the home page.

I expected to see: The page

Instead, I got: Error 500.
Siteground fixed the problem, but suggested I report it to you. See attached screenshot of their reply.

siteground-fix.jpg
January 14, 2021 at 10:14 am #7842457

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hello,

thanks for contacting WPML support.

This sounds to me like an issue we are aware of, and the problem actually comes from certain third-party plugins calling flush_rewrite_rules(true) function too often. This causes WordPress to overwrite .htaccess. However, in some cases, this happens so often, sometimes even on every page request, that it’s called while browsing the front-end in another language.

When this happens, WPML goes ahead and adds the language folder to home_url as usual. This, however, results with an incorrect RewriteBase in .htaccess and ends up breaking the front-end.

The proper fix is for the third-party plugin to flush the rewrite rules only once. This would result in better performance of plugins in question because flushing rewrite rules is a server-expensive operation and extra care should be taken.

However, we can also avoid the language folder from being added within WPML. As a temporary workaround you can add the following lines to the functions.php of your theme:

Temporary workaround for avoiding the language folder from being added

add_filter('mod_rewrite_rules', 'fix_rewritebase');
function fix_rewritebase($rules){
    $home_root = parse_url(home_url());
    if ( isset( $home_root['path'] ) ) {
        $home_root = trailingslashit($home_root['path']);
    } else {
        $home_root = '/';
    }
 
    $wpml_root = parse_url(get_option('home'));
    if ( isset( $wpml_root['path'] ) ) {
        $wpml_root = trailingslashit($wpml_root['path']);
    } else {
        $wpml_root = '/';
    }
 
    $rules = str_replace("RewriteBase $home_root", "RewriteBase $wpml_root", $rules);
    $rules = str_replace("RewriteRule . $home_root", "RewriteRule . $wpml_root", $rules);
 
    return $rules;
}

More info: https://wpml.org/errata/htaccess-is-rewritten-with-language-folder/

Please let me know how it goes and don't hesitate to ask if you find any problem along the way or have doubts or questions about the steps mentioned above, I'll do my best to help you in the best way possible.

Regards,
Drazen

January 17, 2021 at 2:56 pm #7863307

roxanneS

My issue is resolved now. Thank you!