apache - 如何将所有 URL 重定向到不同的域子目录无掩码
问题描述
.htaccess 规则
Redirect 301 "/page-1/" "https://www.newdomain.com/page-1/"
Redirect 301 "/anotherpage/run/" "https://www.newdomain.com/anotherpage/run/"
Redirect 301 "/" "https://www.newdomain.com/subdirectory/"
当前结果
前两个特定页面的规则运行良好。问题在于捕获所有将在目标 url 上进行 URI 的内容。只需要指定 URI 规则以外的任何内容到 301 到新域子目录。
例如。
https://olddomain.com/page-5/ to https://www.newdomain.com/subdirectory/
预期成绩
https://olddomain.com/page-1/ redirects to https://www.newdomain.com/page-1/
https://olddomain.com/anotherpage/run/ redirects to https://www.newdomain.com/anotherpage/run/
https://olddomain.com/anypage/fun/ redirects to https://www.newdomain.com/subdirectory/
我已经尝试了几个没有运气的重写规则。它携带到新域的完整 URI。
RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/$1 [R=301,L]
解决方案
在您的 olddomain.com .htaccess 文件顶部检查此规则
<IfModule mod_rewrite.c>
RewriteEngine On
# custom rewrites
RewriteRule ^page-1/$ https://www.newdomain.com/page-1/ [R=301,L]
RewriteRule ^anotherpage/run/$ https://www.newdomain.com/anotherpage/run/ [R=301,L]
# rewrite all not custom matched requests to https://www.newdomain.com/subdirectory/
RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/ [R=301,L]
</IfModule>
推荐阅读
- laravel - Laravel:验证不是在表的行上找到的特定列值
- python-3.x - 使用python从棘手的文本文件中解析数据-如何将所有相关数据放在一行
- angular - Angular 9 中的 [ngClass]/[class] 和 [className] 有什么区别?
- pine-script - pine-script 中的百分比变化函数
- javascript - 在 JS 中使用 JSON 对象值动态更新 String
- swift - 无法让 CollectionViewCells 在 SWIFT 中工作
- python - 阅读 Javascript 脚本以找出要按下的单选按钮
- r - 从 JSON 日志文件中提取字段值的 R 代码
- c++ - 在运行时环境中使用带有 Lua 的 C++
- java - 如何使用 sched_setaffinity 清除线程关联,这意味着我想将控制权交还给内核?