开发者

htaccess redirect or wp rewrite language wordpress

开发者 https://www.devze.com 2023-04-06 04:31 出处:网络
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 rewri开发者_运维技巧te. The code can be added into the wordp

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 rewri开发者_运维技巧te. The code can be added into the wordpress files or into the .htaccess file


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' );
0

精彩评论

暂无评论...
验证码 换一张
取 消