简体   繁体   English

htaccess重定向TLD保留子域

[英]htaccess redirect TLD preserve subdomains

I want to redirect some TLDs to a specific TLD, and I also want to preserve subdomains. 我想将一些TLD重定向到特定的TLD,并且我还想保留子域。

The followings are some redirect examples I need: 以下是我需要的一些重定向示例:

sub1.domain.net -> sub1.domain.com
sub2.domain.net -> sub2.domain.com
sub3.domain.net -> sub3.domain.com

sub1.domain.org -> sub1.domain.com
sub2.domain.org -> sub2.domain.com
sub3.domain.org -> sub3.domain.com

I have tried this code in my .htacess file: 我已经在我的.htacess文件中尝试了以下代码:

RewriteCond %{HTTP_HOST} !^([a-zA-Z_\-\.(0-9)]*)domain\.com
RewriteRule  ^ http://$1domain.com%{REQUEST_URI} [L,R=301]

but $1 is not recognized as a variable. 但是$ 1不被识别为变量。

$1 is undefined in your example as you are not capturing the uri. $ 1在您的示例中未定义,因为您没有捕获uri。 To use the captured part of the hostaname ,you need %n variable. 要使用主机名的捕获部分,您需要%n变量。 :

RewriteCond %{HTTP_HOST} !example\.com$
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\..+$
RewriteRule ^ http://%1.example.com%{REQUEST_URI} [NE,L,R]

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

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