Rewrite URL based on home_url

时间:2016-09-19 作者:Varun Kumar

我点击的URL是:

home_url(\'18/profile\')
我希望URL执行为:

localhost/wordpress/seller?page=profile
18是卖家页面的页面ID。

此外,当我多次点击上述URL时,URL的最后一部分会不断追加。

add_filter( \'rewrite_rules_array\',\'my_insert_rewrite_rules\' );
add_filter( \'query_vars\',\'my_insert_query_vars\' );
add_action( \'wp_loaded\',\'my_flush_rules\' );

// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( \'rewrite_rules\' );

if ( ! isset( $rules[\'(wordpress)/(\\d*)$\'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}

// Adding a new rule
function my_insert_rewrite_rules( $rules )
{

$newrules = array();
$newrules[\'(wordpress)/(\\d*)$\'] = \'index.php?page_id=$matches[1]&page=$matches[2]\';
return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, \'page\');
return $vars;
}

2 个回复
最合适的回答,由SO网友:Varun Kumar 整理而成

add_filter(\'rewrite_rules_array\', \'insert_custom_rules\');

add_filter(\'query_vars\', \'insert_custom_vars\');


function insert_custom_vars($vars){        

    $vars[] = \'main_page\';       

    return $vars;

}

function insert_custom_rules($rules) {

    $newrules = array();

    $newrules=array(
        \'page-name/(.+)/?\' => \'index.php?pagename=page-name&main_page=$matches[1]\'
    );

    return $newrules + $rules;
}
因此,在get\\u query\\u var()函数的帮助下,我们可以从url中获取值并相应地使用。通过这种方式,我们可以使用Php获得漂亮的URL。

SO网友:Ethan O\'Sullivan

您似乎要重定向:

http://example.com/18/profile

http://example.com/seller?page=profile
如果是,请将以下内容添加到.htaccess 文件位于<IfModule mod_rewrite.c> WordPress创建的标记:

RewriteCond %{HTTP_HOST}
RewriteRule ^18/profile/?$ seller?page=profile [R=301]
您的.htaccess 如果未被其他插件修改,则应如下所示:

# 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]
# Custom Rewrite
RewriteCond %{HTTP_HOST}
RewriteRule ^18/profile/?$ seller?page=profile [R=301]
</IfModule>
# END WordPress

相关推荐