XF 2.3 Route Filter CASE question

MapleOne

Well-known member
On my site we use something called Market Pages and each user gets a custom URL to their page

ie: domain.com/market/username

The problem I am having is the /username part which is obliviously lower case, but I'm trying to figure out if one types...

domain.com/market/UserName how it could then default to domain.com/market/username without getting an oops.

I suppose I could make two route filters, I did that on a test name and it worked but it seems redundant to do that for every user.

On TwitterX it goes through regardless of caps or not so ideally I would like my user names to do similar but they don't have to display in the address bar as caps.

For publishing the address the Capitals can make a nice visual aspect to a user name.
 
I asked chatgpt :D

No idea if this is correct but seems reasonable.




------------------------------------------------------

In XenForo, route filters allow you to customize the way URLs are structured and rewritten. By default, XenForo's routing is case-insensitive, meaning that URLs will work regardless of case. However, if you're looking to implement a custom route filter that enforces or normalizes specific case rules (e.g., always lowercase), you can do so by extending XenForo's existing routing system.

Here's how you can create a custom route filter that ensures URLs are case-insensitive:

### Step 1: Extend the RouteFilter class

You will need to create an add-on or modify an existing one to include your custom route filter logic. This involves extending the RouteFilter class.

1. Create a new PHP file in your add-on under the appropriate directory, usually something like AddOn/YourAddOn/XF/RouteFilter.php.

2. Extend the XF\RouteFilter class:

PHP:
namespace YourAddOn\XF;

class RouteFilter extends \XF\Mvc\Router
{
    public function match($routePath, \XF\Mvc\RouteMatch $routeMatch = null)
    {
        // Normalize the case of the route path
        $routePath = strtolower($routePath);

        // Call the parent match method
        return parent::match($routePath, $routeMatch);
    }
}

### Step 2: Register Your Custom Route Filter

To make sure XenForo uses your custom route filter, you'll need to override the default routing behavior.

1. Register the class extension in your addon.json file or through the XenForo admin interface.

Code:
json
   {
       "extensions": {
           "XF\Mvc\Router": "YourAddOn\XF\RouteFilter"
       }
   }

2. Install or update your add-on through the XenForo admin panel.

### Step 3: Test Your Route Filter

Once everything is set up, test your routes by accessing URLs in different cases (e.g., /forums/ vs. /FORUMS/) to ensure they all resolve correctly and consistently.

### Alternative: Route Filters in Admin Panel

If you prefer not to write code, you can achieve a case-insensitive routing scheme using the existing XenForo route filter options:

1. Go to the Admin Control Panel > Setup > Route Filters.
2. Create a new route filter.
3. Set the "Find route" to the pattern you'd like to match (e.g., Forums).
4. Set the "Replace with" to the lowercase version or desired format (e.g., forums).

This method doesn’t require custom coding but only allows you to map specific routes individually, rather than applying a case-insensitive rule globally.

### Summary

To make your XenForo route filter case-insensitive, you can either create a custom add-on that extends XenForo’s routing logic or use XenForo’s route filter options in the Admin Control Panel. The custom add-on approach gives you more control and flexibility, while the admin panel approach is quicker and easier to implement for specific routes.
 
Back
Top Bottom