2012-06-08 168 views
3

我将从example.com/actionexample.com/index.html#action所有链接,然后通过我的骨干路由器解析。骨干路由器+重写规则不触发与路由“/”

但是,我的新页面signup/:inviteAuthHash(例如signup/9d519a2005b3dac8802d8e9e416239a1)不工作;唯一呈现的是我的index.html,并且我的路由器中没有任何断点符合要求。

的.htaccess:

# not a file or directory 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# example.com/home => example.com/index.html#home 
RewriteRule ^(.+)$ index.html#$1 [L,QSA] 

router.js

var AppRouter = Backbone.Router.extend({ 
    routes : { 
     /* PAGES */ 
     // beta 
     'request_invite' : 'request_invite', 
     'signup/:inviteAuthHash' : 'signup', 

     // Default 
     '*actions' : 'defaultAction' 
    }, 

    /* PAGES */ 
    // eta 
    request_invite : function() { 
     new BetaLayout; 
     new RequestInviteView; 
    }, 
    signup : function(inviteAuthHash) { 
     // validate auth hash 
     if(!InviteRequest.isValidInviteAuthHash(inviteAuthHash)) 
      return this.defaultAction(); 
     else { 
      new BetaLayout; 
      new SignupView(SignupView); 
     } 
    }, 

    // Default 
    defaultAction : function(actions) { 
     app_router.navigate('request_invite'); 
     this.request_invite(); 
    } 
}); 

var initialize = function() { 
    app_router = new AppRouter; 

    $('a').live('click', function() { 
     var href = $(this).attr('href'); 

     // only navigate to real links 
     if(href == undefined) 
      return; 

     // open in new window? 
     if($(this).prop('target') == '_blank') 
      return true; 

     app_router.navigate(href, {trigger: true}); 

     return false; 
    }); 

    Backbone.history.start({pushState: true}); 
}; 
return { 
    initialize : initialize 
}; 
+0

我的mod_rewrite是有点生疏,但不'RewriteRule'实际发送301或303返回给浏览器或者是内部重定向? –

+0

@Garrett我想'注册/ 9d519a2005b3dac8802d8e9e416239a1'将转化到URL'的index.html /#signupindex.html /#9d519a2005b3dac8802d8e9e416239a1'因为两个'/','/'。我可能也是错的。另一个疑问是你的邀请哈希包含字符':'?那么这将不起作用 – Deeptechtons

+1

@ muistooshort - 这取决于您是否使用[R]标志。如果没有,那么它透明地通过隧道(客户端收到200)。 –

回答

2

你可以试试这个的.htaccess来代替:

# not a file or directory 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 
# example.com/home => example.com/index.html#home 
RewriteRule (?!^index\.html)^(.+)$ /index.html#$1 [L,NC,R,NE] 
+0

将我重定向到我的'C:/'目录下的本地文件。 '(?!^ index \ .html)'做了什么? – Garrett

+0

'(?!^ index \ .html)'是一个负面的先行RegEx,它确保只有在开始时URI没有'index.html'才能转发到'index \ .html'。你可以试试:'RewriteRule(?!^ index \ .html)^(。+)$ /index.html#$1 [L,NC,R]' – anubhava

+0

啊,我明白了。对于像我的仪表板'/ dashboard'这样的页面,它会重定向到'/ index。html%23dashboard'(一个无效的网址)。我试过逃避'#'但没有运气。当我删除了'R'时,它开始重新为普通URL创建工作,但没有解决原始问题(对于具有多于1个'/'的URL)。 – Garrett

0

我觉得还有另外一个,在我看来,更好的解决方案到你的问题:使用pushState。

所以,你要做的就是

  1. 改变你的服务器配置,以便example.com下的所有网址,除了你的资产实际呈现的index.html(我的Apache的知识是生锈的,但不应该是很难做到)。
  2. 更改到Backbone.history.start呼叫,能pushState的,并指定根,Backbone.history.start({pushState: true, root: "/"}),看到Backbone documentation

在支持pushState的浏览器,URL将显示它应该和一切工作,对旧浏览器骨干网会自动将其转换为带有散列值的网址。

+0

我已经在做(1)和(2),我没有'root:“/”'所以我补充说,但'/ signup/6efa34df09a031a018dc7627dc8509f7'仍然不起作用 - 我必须将其更改为'#signup/6efa34df09a031a018dc7627dc8509f7'。 – Garrett