首页 > 解决方案 > RedirectMatch 添加额外的 url 段

问题描述

我正在尝试将对我们旧博客的请求重定向到子域上的新 url。旧网址看起来像

https://www.website.com/blog-name/post/slug-of-the-title

它需要重定向到

https://stage.website.com/blog-name/slug-of-the-title

我在我的 .htaccess 中使用这个规则

RedirectMatch ^/blog-name/post/(.*)$ https://stage.website.com/blog-name/$1

我被重定向到正确的页面,但我的网址最后有额外的段。像

https://stage.website.com/blog-name/slug-of-the-title/?/blog-name/post/slug-of-the-title

我究竟做错了什么?

标签: apache.htaccessredirectmod-rewriteurl-rewriting

解决方案


使用您显示的示例,您能否尝试以下操作。请在测试您的 URL 之前清除您的浏览器缓存。确保将这些规则保留在 .htaccess 规则文件的顶部(以防 .htaccess 文件中还有更多规则)。

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?website\.com$ [NC]
RewriteRule ^blog-name/post/(.*)/?$ https://stage.website.com/blog-name/$1? [NC,R=301,L]

推荐阅读