将子域重写为主域,无需使用 htaccess 重定向 [英] Rewrite subdomain to main domain without redirect with htaccess

查看:77
本文介绍了将子域重写为主域,无需使用 htaccess 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I want to rewrite sub.domain.com to domain.com/sub.
But also sub2.domain.com needs to be rewritten to domain.com/sub2 and sub3 etc...
The url is not allowed to change so a redirect is not an option.
My current code is an endless redirect:

RewriteEngine on
RewriteBase /

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

如何更改 .htaccess 以解决此问题.

How can I change the .htaccess to get this fixed.

推荐答案

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

The loop happens because you rewrite to sub.domain.com which then triggers the cond because of the presence of sub. and rewrites again.

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

If you don't want the address to change, you can try using the proxy flag instead. Make sure mod_proxy is enabled:

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

这篇关于将子域重写为主域,无需使用 htaccess 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆