.htaccess htaccess重定向来自多个子域的链接

.htaccess htaccess重定向来自多个子域的链接,.htaccess,redirect,.htaccess,Redirect,我现在的困境是: 我有很多来自多个子域名的链接 https://dev.example.com/abc.html https://xyz.example.com/abc.html 如何使用htaccess重定向它们 重定向301https://dev.example.com/abc.html https://xyz.example.com/abc显然不起作用因为它只能与/abc一起工作 正如我提到的,我在那里有很多链接,出于某种原因,我希望在htaccess中有它 请注意,abc是存在于目标ur

我现在的困境是:

我有很多来自多个子域名的链接

https://dev.example.com/abc.html

https://xyz.example.com/abc.html

如何使用htaccess重定向它们

重定向301https://dev.example.com/abc.html https://xyz.example.com/abc
显然不起作用因为它只能与
/abc
一起工作

正如我提到的,我在那里有很多链接,出于某种原因,我希望在htaccess中有它

请注意,abc是存在于目标url上的有效页面,因此如果我使用
/abc
,它将导致无限循环,因为它将忽略子域

问题不是从子域重定向到另一个,事实并非如此,问题是我有链接,我想从子域重定向到另一个特定的链接

因此,我不是在寻找类似于非www到www的语法

我不希望有这样的东西: 除非可以将某个子域下的所有链接分组
dev.example.com
,然后将其下列出的所有链接后跟目标url

这可以通过
php
实现,但我更喜欢在
htaccess
中实现

我认为最接近的解决方案是将这些链接分散在apache的vHost中

<VirtualHost *>
  ServerName dev.example.com
  Redirect 301 / http://example.com/
  # Have the rest of links here
</VirtualHost>
这就是我从
cpanel
创建重定向时在
htaccess
中得到的。所以它验证了我达成的解决方案


我即将找到解决方案,现在为了便于管理这些,我正在寻找一种方法,将它们全部分组到一个规则容器中。

VirtualHost
节仅允许在Apache配置或vhost配置文件中使用,并且需要
sudo
root
权限来编辑这些文件

在没有这些特权的情况下,为每个子域设置单独的目录总是比较干净的,这样每个子域就可以有一个单独的
.htaccess
。这将允许您将子域的所有规则保存在一个
.htaccess
中。这样就不需要在每个规则之前有
RewriteCond%{HTTP_HOST}
type条件


对于单个.htaccess,您可以使用此替代方法,其中源始终是
example.com
,目标是子域:

RewriteEngine On

# all other rules except redirect links to subdomains go here

# if current domain is not example.com then ignore all the rules below
RewriteCond %{HTTP_HOST} !^(?:www\.)?example\.com$ [NC]
RewriteRule ^ - [L]

# all the redirects to subdomains go below

RewriteRule ^some-faq-item/?$ to https://faq.example.com/some-faq-item [L,NC,R=301]

RewriteRule ^from1/?$ to https://faq.example.com/to1 [L,NC,R=301]

RewriteRule ^abcd/?$ to https://dev.example.com/ [L,NC,R=301]

@anubhava,它可以工作,但这不是我的观点在另一个子域上可能有相同的url,因此/abc将忽略源子域,这将导致无限重定向,比如说
dev.x.com/abc
将重定向到
dev2.x.com/abc
,现在对htaccess来说重要的是/abc,而且exists@anubhava我更新了我的问题,以澄清我的想法,我的客户有一个网站,只有一个www.example.com,现在我们想将内容分为子域,articles.example.com,faq.example.com。。。以此类推,内容仍然存在,我们只是想正确地重定向它们,而且那里碰巧有一些自定义的301 htaccess重定向,它们应该继续重定向,这就是为什么如果我重定向/abc到faq.example.com/abc,如果我只使用/abc作为条件,它将导致无限重定向。我想到了ApachevHosts,将链接分散在每个vhost中,如果是这样的话possible@anubhava我更新了我的问题甚至更多,我接近找到解决方案,我现在唯一的目标是把它们放在一个容器下,如果你可以选择的话!
<VirtualHost *>
  ServerName xyz.example.com
  Redirect 301 / http://example.com/
  # have the rest of the links here
</VirtualHost>
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^tags\/xyz\.html\ $ "https\:\/\/dev\.example\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^abcd$ "https\:\/\/dev\.example\.com\/" [R=301,L]
RewriteEngine On

# all other rules except redirect links to subdomains go here

# if current domain is not example.com then ignore all the rules below
RewriteCond %{HTTP_HOST} !^(?:www\.)?example\.com$ [NC]
RewriteRule ^ - [L]

# all the redirects to subdomains go below

RewriteRule ^some-faq-item/?$ to https://faq.example.com/some-faq-item [L,NC,R=301]

RewriteRule ^from1/?$ to https://faq.example.com/to1 [L,NC,R=301]

RewriteRule ^abcd/?$ to https://dev.example.com/ [L,NC,R=301]