.htaccess重定向动态url

zte4gxcn  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(173)

我有这个.htaccess重定向URL与特定的关键字在URL:

RewriteEngine on

RewriteRule ^testa/(.*)$ https://app.domain2.com/testa/$1 [R=301,NC]
RewriteRule ^testb/(.*)$ https://app.domain2.com/testb/$1 [R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

如果URL包含/testa//testb/,我想重定向到新的域URL:分别为https://app.domain2.com/testa/$1https://app.domain2.com/testb/$1
我有这个URL https://app.domain1.com/testa/page/1,它应该被重定向到https://app.domain2.com/testa/page/1
使用上面的.htaccess代码,我没有得到重定向结果。我想重定向URL与特定的参数名称只,而不是所有的URL从domain1到domain2。

vdzxcuhz

vdzxcuhz1#

RewriteRule ^testa/(.*)$ https://app.domain2.com/testa/$1 [R=301,NC]
RewriteRule ^testb/(.*)$ https://app.domain2.com/testb/$1 [R=301,NC]

您在两个重定向上缺少Llast)标志。因此,处理将继续,最后一个规则将请求重写为index.php?/,并显示错误的301 HTTP状态-由于缺少Location标头,因此不会发生重定向。
您需要在这两个规则/重定向中包含L标志,以便立即触发外部重定向。即[R=301,NC,L]

  • 旁白 *

如果你要重定向到目标站点的同一个URL路径(例如testatesta),那么你可以捕获整个URL路径,而不是只捕获testa(或testb)之后的部分。如果它只是testatestb,那么你只需要一个使用正则表达式替换的规则。例如:

RewriteRule ^(testa|testb)/.* https://app.domain2.com/testa/$0 [R=301,NC,L]

$0反向引用包含与RewriteRule * 模式 * 匹配的完整URL路径。
注意:首先使用302(临时)重定向进行测试,以避免潜在的缓存问题。

相关问题