Rewriting URLs in WordPress

'event/industry/(.+)/?$'

The above is the URL that will be rewritten behind the scenes. The brackets create what is known as a “backreference”.

'index.php?post_type=eg_event&industry=' . $wp_rewrite->preg_index(1)

The above is the actual URL that will be served to the browser.

So, http://yourdomain.com/event/industry/abc/ matches the rule “event/industry/(.+)/?$”. The term ‘abc’ becomes a backreference with index 1, because it matches the term enclosed in brackets.

When someone browses to that URL, the server will then silently rewrite that URL to “index.php?post_type=eg_event&industry=abc”, and serve that page instead. This “rewrite” is not visible to the browser/end-user.

$wp_rewrite->preg_index(1) simply refers to backreference with index 1.

You can have multiple backreferences. For instance:

event/(.+)/(.+)/?$

Now this rule will match http://yourdomain.com/event/magic/def/.

This time, “magic” becomes backreference with index 1, while “def” becomes backreference with index 2.

So you could rewrite the rule to:

index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)

i.e. index.php?post_type=eg_event&magic=def