2016-03-21 8 views
1

、私はルータのこの種ました:URLを管理するにはどうすればいいですか?そして、私興亜アプリで/

  • mydomain.com
  • mydomain.com/
  • はmydomain:

    app 
        .use(router(app)) 
        .all('/', frontRoutes.home.index); 
    

    を私の問題は、ということです。 com?

同じルートでルーティングされます。それは素晴らしいかもしれないが、Googleにとってはそうではない。重複したコンテンツだと言っています。だから私は第1と第3を第2にリダイレクトしたいと思います。このようなもの:

app 
    .use(router(app)) 
    .redirect('/\?', '/', 301) 
    .redirect('', '/', 301) 
    .all('/', frontRoutes.home.index); 

成功していない正規表現を試してみましたか? Githubの問題を既に開いていますが、回答もありません:https://github.com/alexmingoia/koa-router/issues/251

ご協力ありがとうございます。

+0

[ 'パスに-regexp'](https://github.com/pillarjs/path-to-regexp)あなたに渡す値を固定するようです:あなたは、昔ながらのミドルウェアでこれを達成することができますデフォルトでパターン。 '' \\ ''式を試してください。 '.redirect( '\ ??、'/'、301)'のようなものです。 –

+0

バグを解決していないようだ...試してもうまくいかない:(私を助けようとしたあなたのためのThansk – MathKimRobin

答えて

2

koa-routerに問題はありません。

// Redirects "/hello/world/" to "/hello/world" 
function removeTrailingSlash() { 
    return function * (next) { 
    if (this.path.length > 1 && this.path.endsWith('/')) { 
     this.redirect(this.path.slice(0, this.path.length - 1)) 
     return 
    } 
    yield * next 
    } 
} 

// Redirects "/hello/world?" to "/hello/world" 
function removeQMark() { 
    return function * (next) { 
    if (this.path.search === '?') { 
     this.redirect(this.path) 
     return 
    } 
    yield * next 
    } 
} 

// Middleware 

app.use(removeTrailingSlash()) 
app.use(removeQMark()) 
app.use(router(app)) 

// Routes 

app 
    .all('/', frontRoutes.home.index) 

app.listen(3000) 
+0

地獄:)ありがとう!!! – MathKimRobin

+0

少しバグがあります。 'this.originalUrl'で' this.path'を置き換えなければなりませんでした。しかし、残りのすべての作品、おかげで再び! – MathKimRobin