现在是我的两难境地:
我有来自多个子域的大量链接
https://dev.example.com/abc.html
https://xyz.example.com/abc.html
我如何使用htaccess重定向他们
Redirect 301 https://dev.example.com/abc.html https://xyz.example.com/abc
很显然,这是行不通的,它只能与/abc
。
正如我提到的,我在那里有很多链接,出于某种原因,我想在htaccess中使用它
请注意,abc是目标网址上存在的有效页面,因此如果我使用abc,则会导致无限循环,
/abc
因为它将忽略子域。问题不是从子域重定向到另一个,不是这种情况,情况是我有一些链接,我想从子域重定向到另一个特定的链接。
因此,我不是在寻找诸如nonwww到www之类的语法。
RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
除非可以通过将某个子域下的所有链接分组等方法来完成,否则请在其下列出所有链接,dev.example.com
然后是目标URL。
可以做到这一点
php
,但我更喜欢把它放进去htaccess
。
我认为最可行的解决方案是将这些链接分散在apache的虚拟主机中
<VirtualHost *>
ServerName dev.example.com
Redirect 301 / http://example.com/
# Have the rest of links here
</VirtualHost>
然后
<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]
这就是我htaccess
从创建重定向时得到的cpanel
。因此它可以验证我达到的解决方案。
我即将找到解决方案,现在为了易于管理它们,我正在寻找一种将它们全部分组在一个规则容器中的方法。
VirtualHost
部分仅在Apache配置文件或vhost配置文件中允许,并且需要sudo
或root
特权才能编辑这些文件。
在没有这些特权的情况下,为每个子域都具有单独的目录(每个子域都允许有一个单独的目录)总是比较干净.htaccess
。这将使您可以将一个子域的所有规则都保存在一个规则中.htaccess
。这消除了RewriteCond %{HTTP_HOST}
在每个规则之前都需要类型条件的需要。
对于单个.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]
辉煌的做法!