3

我們即將在reactjs部署我們的網站,我們已經(重新)移動了一個網址,但合併到我們的主頁,因此我們將它合併到/[product] 。現在他們說我應該用301回覆/[product]/menu並將其重定向到/[product],我該如何完成該操作以及其他一些頁面呢?react-router我如何使用redirectLocation或301或302狀態

如何使用react-router設置301重定向?我在哪裏設置哪些頁面需要重定向到其他頁面?我現在有這樣的設置:

app.get('*', (req, res) => { 
    // routes is our object of React routes defined above 
    match({ routes, location: req.url }, (err, redirectLocation, props) => { 
    console.log(redirectLocation); 
    if (err) { // something went badly wrong, so 500 with a message 
     res.status(500).send(err.message); 

    // HERE: HOW DO I USE THIS? 
    } else if (redirectLocation) { // we matched a ReactRouter redirect, so redirect from the server 
     console.log('301/302 yeah!'); 
     res.redirect(301, redirectLocation.pathname + redirectLocation.search); 

... 

我使用reactjs並表達也是如此。

編輯添加路由配置。

const routes = { 
    path: '', 
    component: AppComponent, 
    childRoutes: [{ 
    path: '/products', 
    component: ProductsComponent, 
    name: 'products' 
    }, { 
    path: '/:slug', 
    component: ProductComponent, 
    name: 'product' 
    }] 
} 

爲了以防萬一。在此添加答案:

const routes = { 
    path: '', 
    component: AppComponent, 
    childRoutes: [{ 
    path: '/products', 
    component: ProductsComponent, 
    name: 'products' 
    }, { 
    path: '/:slug', 
    component: ProductComponent, 
    name: 'product' 
    }, { 
    path: '/:product/menu', onEnter(nextState, replace) { replace(`/${nextState.params.product}`) } 
    }, { 
    path: '/oldlink/:testId', onEnter(nextState, replace) { replace({pathname: `http://newsite.com/oldlink/some/${nextState.params.testId}`}) } 
    }] 
} 

回答

0

在路線聲明中,使用<Redirect>Example Here

+0

謝謝!如果我有這個設置,我該如何將它翻譯成你給出的例子?更新我的問題以反映我的路由配置。 – index

+0

我可以看到你發佈解決方案。做得好! –

+0

謝謝!再次提問。我該如何做一個絕對路徑?這是說'警告:路徑必須是路徑名+搜索+散列只,而不是一個完全合格的URL像'當我寫絕對路徑替換。 – index