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.
 
On our website users get a free market page which is tied to their user name.

So user JimJones

gets a marketpage address: domain.com/market/JimJones

The problem I am having is we capitalize user names like JimJones

Then the route filter only works as domain.com/market/JimJones or domain.com/market/jimjones and not both.

Is there any way to make the route filter NOT case sensitive so someone could type jimjones or JimJones at the end of the address?
 
I think you have to use the member ID number and not the user name. Look under the route for members and you will get what is used for it. It is something like int:<member_id> or something like that. Sorry, not at home so I can't look what it is.
 
I'm on cloud so no dev mode :(
You can install addons to it right?

Set up a web server-level redirect (e.g., in for Apache or ) that detects lowercase versions and redirects to the properly cased version.
Apache config:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/market/([a-z]+)$
RewriteRule ^market/(.*)$ /market/${ucfirst:$1} [R=301,L]
This is a simplistic example—you’d need to match against actual usernames in your database to get the correct casing.

Writing a custom route listener or controller logic should help.
 
Last edited:
i posted this last year to another user, no idea if they ever implemented it... oh wait, that was you :D

did you ever try this?
 
i posted this last year to another user, no idea if they ever implemented it

There is no access on cloud to do that kind of stuff
 
Back
Top Bottom