简体   繁体   English

.htaccess 重定向动态 url

[英].htaccess redirect dynamic url

I have this .htaccess to redirect URLs with particular keywords in the 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. 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.

I have this URL https://app.domain1.com/testa/page/1 which should be redirect to https://app.domain2.com/testa/page/1 .我有这个 URL https://app.domain1.com/testa/page/1应该重定向到https://app.domain2.com/testa/page/1

Using above .htaccess code I am not getting redirect result.使用上面的.htaccess代码我没有得到重定向结果。 And I want to redirect URL with particular parameter names only and not all URL from domain1 to domain2.而且我想仅使用特定参数名称将 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]

You are missing the L ( last ) flag on the two redirects.您在两个重定向上缺少L ( last ) 标志。 So processing continues and the last rule ends up rewriting the request to index.php?/ with a malformed 301 HTTP status - no redirect occurs since the Location header is absent.因此处理继续进行,最后一条规则最终将请求重写为index.php?/并带有格式错误的 301 HTTP 状态 - 由于Location Z099FB995346F31C749F6E40DB0F395E 不存在,因此不会发生重定向。

You need to include the L flag on these two rules/redirects in order to trigger the external redirect immediately.您需要在这两个规则/重定向中包含L标志,以便立即触发外部重定向。 ie. IE。 [R=301,NC,L] . [R=301,NC,L]


Aside:在旁边:

If you are redirecting to the same URL-path at the target site (eg. testa to testa ) then you might as well capture the entire URL-path, rather than just the part after testa (or testb ).如果您要重定向到目标站点上的相同 URL 路径(例如testatesta ),那么您不妨捕获整个 URL 路径,而不仅仅是testa (或testb )之后的部分。 And if it's just testa or testb then you only need one rule using regex alternation.如果它只是testatestb那么你只需要一个使用正则表达式交替的规则。 For example:例如:

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

The $0 backreference contains the entire URL-path that is matched by the RewriteRule pattern . $0反向引用包含与RewriteRule模式匹配的整个 URL 路径。

NB: Test first with a 302 (temporary) redirect to avoid potential caching issues.注意:首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。

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

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