6

How do I define a case insensitive (part of a) route?

Example:

Any use of uppercase in the fixed part of the route does not work:

I understand how I can make parameters like {parameter} use a regex pattern using ->with(), but that does not help me with the fixed part of the route, like described above.

tereško
  • 58,060
  • 25
  • 98
  • 150
preyz
  • 3,029
  • 5
  • 29
  • 36
  • Strictly speaking, using a parameter and regex *could* help with your specific problem (turn profile into a dynamic param, specify that it must be the word 'profile' case insensitively) however I understand that's far from an ideal solution. Definite stop-gap measure though. – alexrussell Feb 12 '14 at 14:48
  • The fixed part couldn't be case insensitive but if it were a dynamic param you could have done it as you know that already. – The Alpha Feb 12 '14 at 16:08
  • did you try something like `Route::get('{userId}/{profile}','Controller@action')->where('profile','/profile/i');` ? – Gadoma Feb 13 '14 at 09:40

3 Answers3

7

This can be solved by defining routes the following way:

Route::get('/{userId}/{profile}')->with('profile', '(?i)profile(?-i)');

Even smarter, define it as pattern, then it also becomes available in Route groups.

Route::pattern('profile', '(?i)profile(?-i)');
Route::get('/{userId}/{profile}');
preyz
  • 3,029
  • 5
  • 29
  • 36
0

Adding patterns only works on one route at a time, if you want all routes to be case insensitive add this to your /app/filter.php file in the before section:

I wrote a gist which does this: https://gist.github.com/samthomson/f670f9735d200773e543

Edit your app/filters.php to check for uppercase characters in the route and redirect them to a converted route.

S..
  • 5,511
  • 2
  • 36
  • 43
0

For those using Apache you could also do this:

At this the top of your vhost file add

RewriteEngine On
RewriteMap lowercase int:tolower 

and in your .htaccess

RewriteCond $1 [A-Z]
RewriteRule ^(.*)$ /${lowercase:$1} [R=301,L]
Arnaud Bouchot
  • 1,885
  • 1
  • 21
  • 19
  • I haven't tried the mentioned solutions, but just came to think about general conversion of the URL to lower case: Some routes may need mixed upper/lower case, for example when case sensitive parameters are passed along via the URL (e.g. certain hash types etc). – preyz Jun 03 '17 at 16:24