htaccess根据TLD重写URL

时间:2021-10-03 10:50:46

On my website, I am interested in moving my forums from a subdirectory to a subdomain. The forums are currently located at example.com/forums, and I want to move them to forums.example.com. After the move, I want to use htaccess to 301 redirect all forum traffic to the subdomain, but the problem is that I have two TLDs for my site, a .com domain and a .net domain.

在我的网站上,我有兴趣将我的论坛从子目录移动到子域。论坛目前位于example.com/forums,我想将它们移到forums.example.com。移动后,我想使用htaccess 301将所有论坛流量重定向到子域,但问题是我的网站有两个TLD,.com域和.net域。

I am currently trying to redirect traffic using this:

我目前正在尝试使用以下方法重定向流量:

RewriteCond %{HTTP_HOST} !=forums.example.net
RewriteRule ^forums(/(.*))?$ https://forums.example.net/$2 [L,R=301]

RewriteCond %{HTTP_HOST} !=forums.example.com
RewriteRule ^forums(/(.*))?$ https://forums.example.com/$2 [L,R=301]

This only half-works. Regardless of which TLD I visit, it always redirects me to forums.example.net, even if I am visiting from example.com/forums, in which case I want it to go to forums.example.com. How could I achieve this?

这只有一半的作品。无论我访问哪个TLD,它总是将我重定向到forums.example.net,即使我从example.com/forums访问,在这种情况下我希望它转到forums.example.com。我怎么能实现这个目标?

2 个解决方案

#1


3  

You can use this singe rule as very first rule in forums/.htaccess instead of top level .htaccess:

您可以将此单一规则用作论坛/ .htaccess中的第一条规则,而不是*.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^.*$ http://forums.%1/$0 [L,R=301,NE]

For root .htaccess use this rule as your first rule:

对于root .htaccess,请将此规则用作第一条规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^forums(/.*)?$ http://forums.%1$1 [NC,L,R=301,NE]

#2


1  

Please use below rules for the proper redirection to work as you want it.

请使用以下规则进行正确的重定向,以使其正常工作。

Rewritengine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^forums$ http://forums.example.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.net$
RewriteRule ^forums$ http://forums.example.net/? [L,R=301]

#1


3  

You can use this singe rule as very first rule in forums/.htaccess instead of top level .htaccess:

您可以将此单一规则用作论坛/ .htaccess中的第一条规则,而不是*.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^.*$ http://forums.%1/$0 [L,R=301,NE]

For root .htaccess use this rule as your first rule:

对于root .htaccess,请将此规则用作第一条规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^forums(/.*)?$ http://forums.%1$1 [NC,L,R=301,NE]

#2


1  

Please use below rules for the proper redirection to work as you want it.

请使用以下规则进行正确的重定向,以使其正常工作。

Rewritengine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^forums$ http://forums.example.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.net$
RewriteRule ^forums$ http://forums.example.net/? [L,R=301]