【问题标题】:Using .htaccess to rewrite a domain to another domain with the original domains name as a subfolder使用 .htaccess 将一个域重写为另一个域,并将原始域名作为子文件夹
【发布时间】:2014-11-03 19:15:15
【问题描述】:

假设我有一系列域的 A 记录指向相同的 IP 地址:

  • mainsite.com

  • domain1.com

  • domain2.org

  • domain3.io

  • ...还有很多

是否可以使用 .htaccess 将非 mainsite.com 的域重写为

mainsite.com/domain/other-domain-name.tld/

例如: 参观

mainsite.com/pages/blog 

会在不受影响的情况下到达博客。

参观

some-other-domain.com

some-other-domain.com/some/folder/page.html 

将被重定向到

mainsite.com/domain/some-other-domain.com/

非常感谢 =)

【问题讨论】:

  • 嗨@Croises。是的,我已经阅读了该页面。我是使用 .htaccess 的新手,我花了几个小时才阻止我的 mod_rewrite 陷入无限循环。你能帮忙看看实际的语法吗?

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?mainsite\.com$ [NC]
RewriteRule ^ http://mainsite.com/domain%{REQUEST_URI} [NE,R=301,L]

【讨论】:

  • 您好,感谢您的回复。当我尝试这个时,我确实看到重定向发生了,但 %{REQUEST_URI} 似乎以/ 的形式返回,所以wrongdomain.com -> rightdomain.com/
  • 好的,我更新了我的规则以包括/domain。您可以通过单击我的答案左上角的勾号将答案标记为已接受。
【解决方案2】:

在对@anubhava 的代码进行一些修改后:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?mainsite\.com$ [NC]
RewriteRule ^ http://mainsite.com/domain/%{HTTP_HOST} [NE,R=301,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2010-12-28
    相关资源
    最近更新 更多