繁体   English   中英

.htaccess 重定向动态 url

[英].htaccess redirect dynamic url

我有这个.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]  

I want to redirect if the URL contains /testa/ or /testb/ to a new domain URL: https://app.domain2.com/testa/$1 and https://app.domain2.com/testb/$1 respectively.

我有这个 URL https://app.domain1.com/testa/page/1应该重定向到https://app.domain2.com/testa/page/1

使用上面的.htaccess代码我没有得到重定向结果。 而且我想仅使用特定参数名称将 URL 重定向,而不是将所有 URL 从 domain1 重定向到 domain2。

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

您在两个重定向上缺少L ( last ) 标志。 因此处理继续进行,最后一条规则最终将请求重写为index.php?/并带有格式错误的 301 HTTP 状态 - 由于Location Z099FB995346F31C749F6E40DB0F395E 不存在,因此不会发生重定向。

您需要在这两个规则/重定向中包含L标志,以便立即触发外部重定向。 IE。 [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(临时)重定向进行测试,以避免潜在的缓存问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM