Pramod Kumar Sharma
Pramod Kumar Sharma

Reputation: 8012

Custom url in wordpress

I asked same question here

I am struggling to create custom rewrite rules for wordpress..

This is my url structure

http://domain/cityprofile/?city=Sydney 

where city profile is a page template.

Now i want to make this url as

http://domain/Sydney 

Upvotes: 0

Views: 249

Answers (2)

Neeraj
Neeraj

Reputation: 180

Try this, not tested, can work with some testing and changes.

add_action('init', 'add_my_rewrite');
function add_my_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_rule('/([^/]+)/','index.php?pagename=cityprofile?city=$matches[1]','top');
    $wp_rewrite->flush_rules(false);  // This should really be done in a plugin activation
}

Upvotes: 1

Bryan Lusica
Bryan Lusica

Reputation: 11

WordPress allows theme and plugin developers to programmatically specify new, custom rewrite rules. The following functions (which are mostly aliases for WP_Rewrite methods) can be used to achieve this.

WordPress rewrite codex

Example

Let's assume you are creating a "Nutrition" page for showing nutritional information. This page uses a custom template and takes two variables, food and variety. Instead of passing ugly querystring variables to the page, you can set up a rewrite rule to create some custom pretty URLs. See below...

add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');

This example would match a requested URL like this:

example.com/nutrition/milkshakes/strawberry/

...and interpret it to actually mean...

example.com/index.php?page_id=12&food=milkshake&variety=strawberry

NOTE: When using $matches[] to retrieve the values of a matched URL, capture group data starts at 1, not 0.

Upvotes: 1

Related Questions