Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using caseInsensitiveMatch with stateProvider #197

Closed
graphicsxp opened this issue Jun 21, 2013 · 20 comments
Closed

using caseInsensitiveMatch with stateProvider #197

graphicsxp opened this issue Jun 21, 2013 · 20 comments

Comments

@graphicsxp
Copy link

In AngularJS RouteProvider, the attribute caseInsensitiveMatch can be used on each individual routes.

How do I do that with the stateProvider ?

@ksperling
Copy link
Contributor

It's not supported by $stateProvider at the moment.

What's the use case for this? Are you getting users to type in URLs to states manually, or why would the links not end up matching the case of the URL as defined in the state?

@graphicsxp
Copy link
Author

yes, it's exactely that. I've got users typing or pasting a URL which contains upper-case letters and they get a 404.

To workaround this issue, I've used a URL rewrite rule on the server to force lower-case, but then I get a lot of 301 requests, which is not ideal.

In my opinion this is a must-have feature to be able to run case-insensitive requests.

@ksperling
Copy link
Contributor

If they were pasting surely the case would be correct?

Anyway you should be able to do something like this:

$urlRouterProvider.rule(function ($i, $location) {
   var path = $location.path(), normalized = path.toLowerCase();
   if (path != normalized) return normalized;
});

@graphicsxp
Copy link
Author

sometimes the url is copied from some Excel sheet with bad formatting. Anyway, that's not the point. They often type with the caps lock key on too.

Thanks for this code. Where should I use it ? I've tried to put it in the .config() but it fails as $location is undefined :

angular.module('myApp', [deps]).
config(function ($routeProvider, $stateProvider, $urlRouterProvider, $locationProvider) {
    $urlRouterProvider.otherwise('/404');

    $urlRouterProvider.rule(function ($i, $location) {
        var path = $location.path()
        var normalized = path.toLowerCase();
        if (path != normalized) return normalized;
    });

    $locationProvider.html5Mode(true);
 });

@ksperling
Copy link
Contributor

try removing the $i parameter, the arguments for urlRouter rules change between different versions of ui-router

@graphicsxp
Copy link
Author

Yes, that works fine now. Thanks a lot !

@graphicsxp
Copy link
Author

I'm reopening this issue because I've noticed that my base url is case sensitive. This thread on SO explains the issue: http://stackoverflow.com/questions/14994324/angularjs-base-href-case-sensitive

Adding the custom rule to UrlRouteProvider works fine for the part AFTER the base URL. However if the base URL is entered in upper-case, then it does not work because the base URL is defined in lower-case in my index.html page.

The fix provided on SO is for the routeProvider of AngularJS. What should I do to fix this with the $UrlRouteProvide ?

@timkindberg
Copy link
Contributor

@ksperling What is the latest set of parameters for the rule() method? I want to add it to the wiki! It is $injector and $location in the src currently, but are you saying that $injector is now removed?

@vvolodin
Copy link

vvolodin commented Aug 5, 2013

I'm having the same issue with base href being case sensitive.

@devmondo
Copy link

devmondo commented Aug 5, 2013

same goes here

@nateabele
Copy link
Contributor

Plunkr?

@vvolodin
Copy link

vvolodin commented Aug 6, 2013

How can one test base url in Plunker?

@nateabele
Copy link
Contributor

<script>
document.write('<base href="' + location.toString().toUpperCase() + '" />');
</script>

?

@jeme
Copy link
Contributor

jeme commented Aug 7, 2013

Don't think that talk about the matching of Base Ref is in the right place here, ui-router uses $location.$path() which is meant to not include the "base ref" part (AFAIK), meaning that it is more likely to be within the implementation of $location the problem lies.

@nateabele
Copy link
Contributor

@jeme Ordinarily I would agree, but there seems to be equivalent functionality in the Angular core router.

@jeme
Copy link
Contributor

jeme commented Aug 7, 2013

@nateabele Not for base path handling, or well I can't find the code in their router that does that. As far as I can tell from their code they also just relies on what $location.$path() returns. And so I won't be terrible surprised if their router suffers from that as well.

@nateabele
Copy link
Contributor

Okay, agreed that this is a $location issue.

@mauroservienti
Copy link
Contributor

I'm trying to understand what is the use case of having case sensitive url, I have lots of cases where case sensitive urls are an issue.
Why should /Users be different from /users? do they identify 2 different resource? I suppose that no human body on earth will tell that, only computers :-)

why not simply do:
this.regexp = new RegExp(compiled, 'i');

instead of
this.regexp = new RegExp(compiled);

.m

@timkindberg
Copy link
Contributor

@mauroservienti feel free too submit a PR, I'm not opposed to having case insensitive urls

@mauroservienti
Copy link
Contributor

PR done :-) it’s my first time, so be patient :-P

On 09/mar/2014, at 03:23, Tim Kindberg <notifications@github.commailto:notifications@github.com> wrote:

@mauroservientihttps://github.com/mauroservienti feel free too submit a PR, I'm not opposed to having case insensitive urls


Reply to this email directly or view it on GitHubhttps://github.com//issues/197#issuecomment-37116987.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants