1

The URL I am hitting is:

home_url('18/profile')

I want URL to be executed as:

localhost/wordpress/seller?page=profile

18 is the page ID for seller page.

Also, when I hit on above said URL multiple time then last part of URL appending continuously.

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;
}
0

It looks like you want to redirect:

http://example.com/18/profile

to

http://example.com/seller?page=profile

If so, add the following to your .htaccess file in between the <IfModule mod_rewrite.c> tags that were created by WordPress:

RewriteCond %{HTTP_HOST}
RewriteRule ^18/profile/?$ seller?page=profile [R=301]

Your .htaccess should looks like the following if it hasn't been modified by another plugin:

# 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
  • Thanks for your time @Ethan Jinks O'Sullivan. But the problem is I don't want to do this using .htaccess. Is there any way to do this by adding rules using php, like the code mentioned in question. – Varun Kumar Sep 19 '16 at 8:34
0
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;
}

So, with the help of get_query_var() function, we can get value from url and use accordingly. In this way, we can get pretty URLs using Php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.