0

How can I rewrite URLs on WordPress?

For example, I have a URL like sitename.com/?page=<numbers>&id=<numbers> and I want to rewrite it as sitename.com/page/id/<other info>. How can I do that? I don't understand the rules!

1 Answer 1

0

Your best bet for an easy solution is to actually use the "Custom Structure" option in your WordPress admin under Settings > Permalinks.

There are a number of other structure tags you may use as seen in the Codex.

If you're looking for even more customization and manual control, it's actually a pretty complex topic involving editing your .htaccess file. This great two part tutorial by Stephan Harris covers this topic very well. (Better than I ever could here on SO).

3
  • So, the rule might be (in my problem) add_rewrite_rule('^([0-9]*)/^([0-9]*)','index.php?page=$matches[1]&id=$matches[2]','top'). Isn't it?
    – apanzett
    Jun 1, 2013 at 15:20
  • @apanzett Well, the expression you have there will try to match numerics for the "page" portion. You might try something like add_rewrite_rule('^([^/]*)/^([0-9]*)','index.php?page=$matches[1]&id=$matches[‌​2]','top'). Notice the slight difference is that the first section will match all strings (since page names are typically words and/or numbers). I've found Debuggex to be a pretty helpful tool for playing with regex.
    – Jared Cobb
    Jun 1, 2013 at 16:23
  • And if I have a known page, i'll try something like ^PAGENAME/([0-9]*)/([^/]*)/([0-9]*) => index.php?page_id=$matches[1]&event=$matches[2]&id=$matches[3] where page_id and id are only numbers and event is string composed?
    – apanzett
    Jun 2, 2013 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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