mod-rewrite - 重写中间路径的重写规则

标签 mod-rewrite

我需要编写一个重写规则的代码,以便文章来自:

www.olddomain.com/categoryA/categoryB/article.html

被重定向到:
www.newdomain.com/categoryC/categoryD/article.html

所以这里的挑战是 article.html 页面不会从一个站点更改到另一个站点,但中间路径(类别)不同。
我试过编码这个规则:
RewriteRule ^(categoryA/categoryB/.+)$ http://www.newdomain.com/categoryC/categoryD/$1 [L,NC,R=301,NE]

但是,规则重定向自:
http://www.olddomain.com/categoryA/categoryB/article.html


http://www.newdomain.com/categoryC/categoryD/categoryA/categoryB/article.html

您能建议我在执行重定向时排除“旧”类别的方法吗?我希望被重定向到:
http://www.newdomain.com/categoryC/categoryD/article.html

非常感谢

最佳答案

就这么简单:

RewriteRule ^categoryA/categoryB/([^/]+)\.html$ http://www.newdomain.com/categoryC/categoryD/$1.html [R=301,L,NC,NE]

关于mod-rewrite - 重写中间路径的重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61367472/

相关文章:

apache - 重写规则以从 Rails 服务器的 URL 隐藏端口?

使用 https 重写 Apache 2.4 url

使用 HTTP->HTTPS Apache 重定向时,Javascript `document.referrer` 消失

.htaccess - 替换长 URL 的一部分并重定向

php - 使用 htaccess 从 index.php 重定向到 another.php 文件

php - mod_rewrite 最佳实践

java - 如何通过 mod_rewrite 将请求传递给 mod_jk 并剥离应用程序上下文,让 tomcat 5.5 在 apache 2 后面运行?

apache - 如何将子域映射到服务器上的文件夹?

linux - Apache 将所有 https (443) 请求重定向到外部服务器

apache - 如何使用自定义 url 方案处理 mod-rewrite?