如何重写htaccess中的子域



我试图简化对用户的子域复杂性感知,所以我想屏蔽子域。

我的目标如下:

当用户试图到达这里

blog.example.com/blog/whatever

我想将子域掩码为

example.com/blog/whatever

提供相同的内容,但隐藏了子域。

我尝试了以下方法,但没有成功。

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^https://blog.example.com/blog [NC]
RewriteRule ^(.*)$ http://example.com/blog/$1 [R=301,L]
</IfModule>

更新:重写模块已启用

RewriteCond %{HTTP_HOST} ^https://blog.example.com/blog [NC]
RewriteRule ^(.*)$ http://example.com/blog/$1 [R=301,L]

HTTP的Host标头(即。(HTTP_HOST服务器变量的值)仅包含主机名,即。blog.example.com。它不包含完整的绝对URL。

所以,你的规则应该是这样的:
RewriteCond %{HTTP_HOST} ^blog.example.com [NC]
RewriteRule ^blog($|/) https://example.com%{REQUEST_URI} [R=301,L]

这匹配url路径/blog/blog/<whatever>(但不是/blog<something>),并重定向到example.com(+ HTTPS)的相同url路径。REQUEST_URI服务器变量包含根相对url路径,以斜杠开始。

你重定向到HTTP,但我认为这一定是HTTPS?

你应该在测试前清除浏览器缓存,并首先使用302(临时)重定向进行测试,以避免潜在的缓存问题。