【问题标题】:htaccess redirect TLD preserve subdomainshtaccess 重定向 TLD 保留子域
【发布时间】:2017-08-20 09:16:05
【问题描述】:

我想将一些 TLD 重定向到特定的 TLD,并且我还想保留子域。

以下是我需要的一些重定向示例:

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

我已在我的 .htacess 文件中尝试过此代码:

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

但 $1 未被识别为变量。

【问题讨论】:

    标签: php apache .htaccess redirect


    【解决方案1】:

    $1 在您的示例中未定义,因为您没有捕获 uri。要使用主机名的捕获部分,您需要 %n 变量。 :

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-08
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2015-02-03
      相关资源
      最近更新 更多