Ning Help Center

URL Rewriting

URL Rewriting

5 (100%) 2 votes


Web sites benefit greatly from giving their pages short, simple and understandable URLs. Unfortunately, PHP isn’t well-disposed towards keeping URLs simple. This is where Ning’s URL Rewriting feature comes in. You can create new rewrite rules for your Site that transform incoming URLs before they’re processed.  To start working with the rewrite rules / URL mappings of your social network access the tool here:

http://examplenetwork.ning.com/main/admin/urlMappings
where you replace examplenetwork with the .ning.com subdomain of your social network.

Once you’re there we can create a URL rewrite rule that will allow us to use the latter style of URLs in the Site, while keeping the Site code the same. The rewrite rule will transform the URL from the latter style to the former before it’s processed.  If you’re familiar with Apache’s mod_rewrite you’ll find this familiar.

Each regular expression rewrite rule has a pattern (which matches against the incoming URL) and a target (which produces the transformed URL). Here’s the rule to do what we need:

Pattern
/user/(\w+)
Target
/index.php?controller=user&view=default&user=$1

The pattern above is written as a Java-compatible regular expression. (For more information about regular expressions, check out this tutorial.) The pattern matches against any relative URL that looks like /user/ followed by the alphanumeric characters that make up the username. This username is grabbed through grouping (i.e. being surrounded by parentheses) and placed in the group $1. This group is then fetched in the target and placed at the end of the new URL.

Note that patterns match against relative URLs – that is, URLs with the opening host section removed. Because of this, you cannot rewrite a URL to point at a different host or domain. (This can be done, but requires additional PHP code.) Also note that rewrite rules are applied in the order in which they’re displayed.

The above pattern will work, but has the disadvantage that any additional query string parameters will be lost. Here’s an example of the kind of URL we’re talking about:

http://site.ning.com/user/martin?page=2

In the rule above, everything after martin would be discarded. So here’s a better rule that captures the query string parameters as well, and appends them to the transformed URL:

Pattern
/user/(\w+)\??(.*)?
Target
/index.php?controller=user&view=default&user=$1&$2

Similar Articles: