Sergio Pellegrini
Sergio Pellegrini

Reputation: 87

htaccess redirect or wp rewrite language wordpress

Would like to know how it is possible to rewrite www.site.com/?lang=de to www.site.com/de in wordpress using the htaccess rewrite or the wp rewrite. The code can be added into the wordpress files or into the .htaccess file

Upvotes: 1

Views: 965

Answers (1)

user7675
user7675

Reputation:

Here's a way to accomplish this within WordPress:

function stack7478067_init() {                                                                                                                                                       
    // Let ?lang= pass through to WP_Query                                                                                                                                           
    add_rewrite_tag( '%lang%', '([A-Za-z]+)' );                                                                                                                                      
}                                                                                                                                                                                    
add_action( 'init', 'stack7478067_init' );                                                                                                                                           

function stack7478067_pre_get_posts( $query ) {                                                                                                                                      
    if ( 'de' == $query->get('lang') ) {                                                                                                                                             
        // if lang=de, redirect                                                                                                                                                      
        die( wp_redirect( site_url( 'de/' ) ) );                                                                                                                                     
    }                                                                                                                                                                                
}                                                                                                                                                                                    
add_filter( 'pre_get_posts', 'stack7478067_pre_get_posts' );

Upvotes: 1

Related Questions