RECENT UPDATES:
27-May-2025 | v7.2.4 | All new article

Change Log

  • 27-May-2025 | v7.2.4 | All new article

Contributors:

Adam Wilson - Logo Pogo

URL Rewrites

The URL Rewrite feature offers powerful control over how URLs are presented and processed within your application. This documentation article will detail what the feature does, provide practical use cases, and guide you through creating effective rewrite rules using Regular Expressions (RegEx).

Found under 'Settings' > 'URL Rewrites' in the admin, utilizing the URL Rewrite feature and understanding the power of Regular Expressions, you may be able to enhance your website's architecture, user experience, and search engine performance.

What is URL Rewriting?

URL rewriting is a technique used to modify the appearance of URLs in your web application. Instead of displaying the actual path to a resource, a rewritten URL presents a more user-friendly, search engine-friendly, or aesthetically pleasing version. When a user requests a rewritten URL, your application internally translates it to the actual path to serve the correct content, without the user ever seeing the underlying structure.

This feature allows you to define rules that match incoming URLs (the "Rewritten URL") and transform them into a different internal URL (the "Rewrite To").

Key Features and Functionality

The URL Rewrite feature allows you to manage rewrite rules with the following configurable fields:

Name
Description
Rewritten URL

This is the incoming URL pattern that you want to match. You must use Regular Expressions (RegEx) to define this pattern. This field is mandatory.
Example: ^article/([0-9]+)/([_0-9a-z-]+)

Rewrite To

This is the internal URL path that the Rewritten URL will be transformed into. You can also use Regular Expressions (RegEx) here, specifically for back-referencing captured groups from the Rewritten URL. This field is mandatory.
Example: /article/article-detail-name

Enabled

A toggle (default: true) that determines whether the rewrite rule is active. You can disable a rule without deleting it, making it easy to temporarily deactivate rewrites.

URL rewrites only function within the same domain as your application. They cannot be used to redirect or rewrite URLs to external domains. For domain-level redirects, refer to your application's domain redirect configuration.

Use Cases for URL Rewrites

URL rewriting is a versatile feature with numerous applications to enhance your website's functionality and user experience. Here are some common use cases:

1Creating SEO-Friendly URLs:

  • Problem: Your application might generate URLs with parameters or complex structures (e.g., www.example.com/product.php?id=123&category=electronics).
  • Solution: Rewrite these into clean, descriptive URLs that are easier for search engines to crawl and users to understand (e.g., www.example.com/products/electronics/awesome-gadget).
  • Example:
    • Rewritten URL: ^product\.php\?id=([0-9]+)&category=([a-z]+)
    • Rewrite To: /products/$2/product-id-$1 (Assuming you have a mapping for product names or want to include the ID for uniqueness)

2Maintaining Backwards Compatibility for Old URLs:

  • Problem: You've restructured your website, changing the paths of existing pages, but old links or bookmarks still point to the original URLs.
  • Solution: Create rewrite rules to seamlessly 'rewrite' old URLs to their new locations.
  • Example:
    • Rewritten URL: ^old-articles/([0-9]+)
    • Rewrite To: /new-blog/article/$1

3Hiding File Extensions:

  • Problem: You want to hide file extensions like .php, .html, or .aspx for a cleaner look and better security.
  • Solution: Rewrite requests for the extension-less URL to the actual file.
  • Example:
    • Rewritten URL: ^about$
    • Rewrite To: /about.html

4Implementing Vanity URLs or Aliases:

  • Problem: You want short, memorable URLs for specific campaigns or sections of your site.
  • Solution: Create a simple rewrite that points the vanity URL to a more complex internal path.
  • Example:
    • Rewritten URL: ^promo-spring-sale$
    • Rewrite To: /promotions/seasonal/2025/spring-campaign

5Canonicalizing URLs (e.g., forcing trailing slashes or removing them):

  • Problem: Your website can be accessed via www.example.com/page and www.example.com/page/, potentially causing duplicate content issues for SEO.
  • Solution: Enforce a consistent URL structure by rewriting one form to the other.
  • Example (forcing trailing slash):
    • Rewritten URL: ^(.*[^/])$
    • Rewrite To: $1/

How to Create Rules Using Regular Expressions (RegEx)

Regular Expressions (RegEx) are a powerful tool for pattern matching in text. Mastering RegEx is crucial for effectively using the URL Rewrite feature.

Basic RegEx Concepts for URL Rewrites

  • Literals: Most characters match themselves directly. For example, article in a RegEx will literally match "article".
  • Metacharacters: Special characters with specific meanings.
    • .: Matches any single character (except newline).
    • *: Matches the preceding character zero or more times.
    • +: Matches the preceding character one or more times.
    • ?: Matches the preceding character zero or one time.
    • ^: Matches the beginning of the string.
    • $: Matches the end of the string.
    • [ ]: Character set. Matches any one character within the brackets.
      • [0-9]: Matches any digit from 0 to 9.
      • [a-z]: Matches any lowercase letter from a to z.
      • [A-Z]: Matches any uppercase letter from A to Z.
      • [_0-9a-z-]: Matches underscore, digit, lowercase letter, or hyphen.
    • ( ): Capturing Group. This is critical for URL rewrites. Anything matched within parentheses is "captured" and can be referenced in the Rewrite To field.
    • |: OR operator. Matches either the expression before or after the |.
    • \: Escape character. Used to treat a metacharacter as a literal character. For example, \. matches a literal dot.

Referencing Captured Groups in "Rewrite To"

When you use capturing groups () in your Rewritten URL RegEx, you can refer to the matched content in the Rewrite To field using $n, where n is the number of the capturing group (starting from 1).

  • $1: Refers to the content matched by the first capturing group.
  • $2: Refers to the content matched by the second capturing group, and so on.

Examples of RegEx in URL Rewrites

Let's break down some examples to illustrate how to construct effective rewrite rules:

1Rewriting a simple ID-based URL
  • Goal: Transform /products/view?id=123 to /products/123.
  • Rewritten URL: ^/products/view\?id=([0-9]+)$
    • ^: Matches the beginning of the URL.
    • /products/view\?id=: Matches the literal string /products/view?id=. The ? is escaped with \ because it's a metacharacter.
    • ([0-9]+): Capturing group 1. Matches one or more digits (0-9+) and captures them. This will capture 123.
    • $: Matches the end of the URL.
  • Rewrite To: /products/$1
    • /products/: Literal string.
    • $1: Inserts the content captured by the first group (which is 123).
2Rewriting an article URL with ID and slug
  • Goal: Transform /article/123/my-awesome-article to /articles/detail/123/my-awesome-article.
  • Rewritten URL: ^article/([0-9]+)/([_0-9a-z-]+)$
    • ^article/: Matches the literal string article/.
    • ([0-9]+): Capturing group 1. Matches one or more digits (the article ID).
    • /: Matches the literal slash.
    • ([_0-9a-z-]+): Capturing group 2. Matches one or more underscores, digits, lowercase letters, or hyphens (the article slug).
    • $: Matches the end of the URL.
  • Rewrite To: /articles/detail/$1/$2
    • /articles/detail/: Literal string.
    • $1: Inserts the captured article ID.
    • $2: Inserts the captured article slug.
3Optional trailing slash
  • Goal: Allow both /about and /about/ to map to /about.html.
  • Rewritten URL: ^/about/?$
    • ^/about: Matches the literal /about.
    • /?: Matches the / zero or one time (making it optional).
    • $: Matches the end of the URL.
  • Rewrite To: /about.html
4Rewriting based on a keyword
  • Goal: Rewrite any URL starting with /blog/ to /weblog/.
  • Rewritten URL: ^/blog/(.*)$
    • ^/blog/: Matches the literal string /blog/.
    • (.*): Capturing group 1. Matches any character (except newline) zero or more times until the end of the string. This captures the rest of the URL path.
    • $: Matches the end of the URL.
  • Rewrite To: /weblog/$1
    • /weblog/: Literal string.
    • $1: Inserts the captured part of the URL.

It is highly recommended to test your Regular Expressions before implementing them in your rewrite rules. Numerous online RegEx testers are available (e.g., regex101.com, regexr.com) that allow you to input your RegEx and test strings, showing you exactly what matches and what is captured.

Best Practices for URL Rewrites

  • Keep it Simple: Start with the simplest RegEx that achieves your goal. Overly complex RegEx can be harder to understand and maintain.
  • Order of Rules: It's generally good practice to place more specific rules before more general ones if there's any chance of overlap.
  • Test Thoroughly: Always test your rewrite rules for all possibilities/conditions. Check for unexpected redirects or broken links.
  • Monitor Performance: While URL rewrites are generally efficient, a very large number of complex rules could theoretically impact performance. Monitor your application's response times if you have a significant number of rules.
  • Avoid Chaining Rewrites: While not explicitly prevented, chaining multiple rewrites (where one rewritten URL is then rewritten again by another rule) can make debugging difficult and is generally discouraged. Strive for a single rewrite per original URL.
  • Consider SEO Implications: Ensure your rewrites lead to clean, descriptive URLs that improve your site's search engine visibility. Be mindful of canonical URLs if you have multiple paths leading to the same content.