dounianxie2058 2017-04-08 12:07
浏览 12
已采纳

使用动态选项重定向

I have a dozen links from an old website I need to redirect to a new path. This is the old URL buildup:

/news/item/{id}-{name}

Which needs to be redirected to:

/blog/item/{id}-{name}

Now I hear you thinking, just swap the news with the blog in your redirect. Well that could be if it was that simple, but as you can imagine, the old News category contained also the news items, and because it's an old website they never split these items into different category's. So I need to identify when I can redirect it to the blog page. Luckily every blog post contains a static string, which we can use to identify the url (I think).

So you have the following URLS:

/news/item/{id}-{name}
/blog/item/{id}-{name}

The static word that is in the {name} of the url is blogpost-name , but can I use that to identify the URLS to redirect to the /blog/ page?

2 Examples:

/news/item/33018-It-was-a-great-day-to--ride
/news/item/29110-Blogpost-author-john-doe-time-to-come-apart

Here we got 2 items in the news url, but only 1 of them, the actual blogpost can only be switched. The other URL needs to stay intact and working.

  • 写回答

1条回答 默认 最新

  • dongying3830 2017-04-08 13:31
    关注

    To redirect all blog posts from /news/... to /blog/..., except those being "news", you must identify them by "-Blogpost-", capture the part you want to keep (...) and reuse this in the target URL $1

    RewriteRule ^news/(item/\d+-Blogpost-.+)$ /blog/$1 [R,L]
    

    When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)