【问题标题】:How to rewrite a subdomain in htaccess如何在 htaccess 中重写子域
【发布时间】:2021-12-10 21:23:54
【问题描述】:

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

我的目标如下:

当用户试图到达这里时

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>

更新:重写模块已启用

【问题讨论】:

  • 您是否发现这些规则有任何错误?或者除了这些规则之外,您的 htaccess 中还有其他规则吗?请让我们知道这些问题。

标签: .htaccess mod-rewrite url-rewriting


【解决方案1】:
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/&lt;whatever&gt;(但不匹配 /blog&lt;something&gt;)并重定向到位于 example.com(+ HTTPS)的相同 URL 路径。 REQUEST_URI 服务器变量包含根相对 URL 路径,以斜杠开头。

您正在重定向到 HTTP,但我认为这一定是 HTTPS?

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

【讨论】:

  • 对,你没看错,应该是https
  • @Imnl 你是怎么做到的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 2011-02-04
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 2011-05-25
相关资源
最近更新 更多