Redirect normal url to hashed url?

691 views
Skip to first unread message

Tronic

unread,
May 22, 2012, 1:10:33 PM5/22/12
to sam...@googlegroups.com
hi,

as i'm rather new to sammy.js, i'm asking myself, how i get sammy.js to redirect an url like http://www.foo.bar/work to http://www.foo.bar/#/work without using some kind of serverside redirect (e.g. htaccess). any advice here?

thanks in advance!

alexandroid

unread,
May 23, 2012, 5:06:02 AM5/23/12
to Sammy.js
It has nothing to do with Sammy - how would you do a redirect from
www.foo.bar/work/index.html to www.foo.bar/index.html? Meta refresh
perhaps? http://en.wikipedia.org/wiki/Meta_refresh#For_redirection

On May 22, 10:10 am, Tronic <clemens.kro...@gmail.com> wrote:
> hi,
>
> as i'm rather new to sammy.js, i'm asking myself, how i get sammy.js to
> redirect an url like *http://www.foo.bar/work*to *http://www.foo.bar/#/work
> * without using some kind of serverside redirect (e.g. htaccess). any

Daniel Dotsenko

unread,
May 23, 2012, 4:23:42 PM5/23/12
to sam...@googlegroups.com
Yeah.

One of the reasons we never switched to Sammy 0.7.x here - not point. History PushState'ed URLs were confusing the hell out of the users (and the server) when they tried to copy and paste URLs without "#" in them. 

I admit something as simple as a URLRewrite rule on server could handle this case, but it somehow felt as an unclean solution.

Daniel.

Ramiro

unread,
May 24, 2012, 3:01:31 AM5/24/12
to sam...@googlegroups.com
The thing I love the best about 0.7 is HTML5 history support. :)
It was only confusing for the developers without a reverse proxy but for the users it has always been transparent.

Daniel Dotsenko

unread,
May 24, 2012, 11:30:04 AM5/24/12
to sam...@googlegroups.com
PushState is, essentially, only supported on FireFox and Chrome


We have a small army of IE8 users. It is rather routine that URI to some resource is being copied into emails and sent around. We also have an insurgence of Chrome users (who do that in defiance of corporate policy). "#" less URIs were, in essence, un-copy-abale and never seen by majority of users because to support "#"-less URIs we put in a URLRewrite to inject "#" at right point. Management of these rewrite rules was getting nuts. Trial users were not sure what URIs to put in the manuals etc, etc. 

Simplicity (for all users and devs) and compatibility (across all browsers) of "#"-containing URIs just made most business sense. Incidentally, i did not see a single instance of a user "loving" PushStated URIs, as compared to "#" URI. At most, they don't give a shit about a difference. 

Daniel.

deitch

unread,
Jun 13, 2012, 2:23:35 AM6/13/12
to sam...@googlegroups.com
I do the following if I want to support both html5 pushState and html4 hash:

        this.around(function(cb){
          var ctx = this,   re = /^\/(#\/)?(([^\/\?]+)(\/.*)?)?/,
          locn = ctx.app.getLocation(), path = locn.match(re), hasHash = path && path[1] !== undefined && path[1] !== null;
          
          // do we need to change our path for /#/ for html4, or / for html5?
          if (hasHistory && hasHash) {
            ctx.redirect("/"+path[2]);
          } else if (!hasHistory && !hasHash){
            ctx.redirect("/#"+locn);            
          } else {
            cb();
          }
        });

Ramiro

unread,
Jun 14, 2012, 5:25:52 AM6/14/12
to sam...@googlegroups.com
I do the following to support both:

this.bind('run', function() {
  // Use #!/ links when needed
  if(!Modernizr.history) {
    var ctx = this;
    $('body').on('click', 'a[href!="#"]', function(e) {
      if (this.hostname === window.location.hostname && this.target !== '_blank' && this.pathname && !this.pathname.startsWith('javascript')) {
        e.preventDefault();
        ctx.redirect('/#!/' + this.pathname);
        return false;
      }
    });
  }
});

Make sure all links are without hashes in your page.
This works for IE and FF.
Reply all
Reply to author
Forward
0 new messages