htAccess 根据查询字符串重定向到子域


htaccess redirect to subdomain based on query string

我搜索了类似的问题,但没有一个适用于我的情况。

我需要做的是从具有特定查询字符串的旧网址重定向:option=com_virtuemart到子域:http://parts.domain.com

尝试了一堆规则和条件,但没有实现正确的重定向。它总是将我重定向到http://parts.domain.com/index.php?option=com_virtuemart

我尝试过的一些规则:

RewriteCond %{QUERY_STRING} (^|&)option=com_virtuemart(&|$) [NC]
RewriteRule (.*) http://parts.domain.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)option=com_virtuemart(&|$) [NC]
RewriteRule http://parts.domain.com/? [L,NC,R=301]

就像其他 50 条规则一样。我在这里犯了什么错误?

如果你在parts.domain.com根目录上,那么你可以在你的.htaccess中执行此操作。

这对我有用,你真的不需要一个可以使用^的捕获组。还要确保清除缓存。

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)option=com_virtuemart(&|$) [NC]
RewriteRule ^ http://parts.domain.com/? [L,NC,R=301]

或者你可以做这样的事情。

RewriteEngine On
RewriteCond %{THE_REQUEST} [A-Z]{3,}' /+(index'.php)?'?option=com_virtuemart [NC]
RewriteRule ^ http://parts.domain.com/? [R=301,L]

你只需要组合你的规则。

RewriteCond %{QUERY_STRING} (^|&)option=com_virtuemart(&|$) [NC]
RewriteRule (.*) http://parts.domain.com/? [L,NC,R=301]

在第二个示例中,您没有提供要匹配的正则表达式。