如何使RewriteRule子域不可知?

时间:2014-12-05 17:23:29

标签: apache .htaccess mod-rewrite

@anubhava的帮助下,我有一个很好的RewriteRule工作。

short.dev  redirects to longer.dev
short.com  redirects to longer.com

使用:

RewriteCond %{HTTP_HOST} ^(?:www\.)?short\.(.+)$ [NC] 
RewriteRule ^ http://longer.%1%{REQUEST_URI} [R=301,L]

我现在需要扩展此规则以使其与子域无关 - 例如:

www.short.dev redirects to www.longer.dev
www.short.com redirects to www.longer.com

es.short.dev redirects to es.longer.dev
es.short.com redirects to es.longer.com

1 个答案:

答案 0 :(得分:1)

您可以将第一个组设为捕获组,然后在后面引用中使用它:

RewriteCond %{HTTP_HOST} ^(www\.)?short\.(.+)$ [NC] 
RewriteRule ^ http://%1longer.%2%{REQUEST_URI} [R=301,L,NE]