2012-06-08 166 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