无需使用 htaccess 重定向将子域重写为主域

Rewrite subdomain to main domain without redirect with htaccess

您好,我想将 sub.domain.com 重写为域。com/sub。
但也 sub2.domain.com 需要重写为域。com/sub2 和 sub3 等...
url 不允许更改,因此不能选择重定向。
我当前的代码是无限重定向:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^(.*?)/?$ http://sub.domain.com/ [L]

如何更改 .htaccess 以修复此问题。

循环发生是因为您重写了 sub.domain.com 然后由于 sub. 的存在触发了 cond 并再次重写。

如果您不想更改地址,可以尝试使用代理标志。确保 mod_proxy 已启用:

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^(.*)/?$ http://domain.com/%1/ [P]