cantera
cantera

Reputation: 25005

Backbone: Direct Page Access Without Hash

Answering my own question to an issue I kept running into with Backbone.js...

In my Backbone applications, I want users to be able to navigate directly to a an interior "page" without having to type a hash (#) in the URL. Out of the box, Backbone Boilerplate doesn't seem to support this even though it does support pushState.

Users already could reach any location as long as they start at the homepage, obviously. Or they could skip straight to an interior page, but only if there was a hash (#) directly after the domain or immediately after an initial slash.

I found a solution to this problem that seems to be working. I'm posting it here for others with the same problem, to see if anyone knows a better way, and because I couldn't find this exact problem addressed elsewhere.

Upvotes: 0

Views: 705

Answers (1)

cantera
cantera

Reputation: 25005

After trying to hack Backbone Boilerplate for a while, it occurred to me that BBB just needs the hash to be there if it isn't already.

So my solution is to append a rewrite rule to my .htaccess file that says if the requested page isn't found, try again with a hash after the domain.

<IfModule mod_rewrite.c>
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ /#$1 [R,L,NE]
</IfModule>

So far it seems to be working fine. This had been bugging me for a while, and I figured others might be in the same boat.

If anyone knows a better way to accomplish this, or any problems my solution may cause, please feel free to respond.

Upvotes: 1

Related Questions