【问题标题】:How to redirect my sudomains webspace to the actual subdomain?如何将我的 sudomains 网络空间重定向到实际的子域?
【发布时间】:2019-10-11 12:46:04
【问题描述】:

我想将https://example.com/sub/index.php 重定向到https://sub.example.com/index.php

如您所知,子域网站空间位于实际的域网站空间中;这会导致内容重复。

我已经试过了:

  1. 在 index.php 的头部添加 javascript:

    <script type='text/javascript'>document.location.href='https://example.com/sub/index.php';</script>
    

    但这会造成无限循环。

  2. 将 .htaccess 添加到 /sub/ 目录:

    RewriteEngine On
    RewriteRule (.*) https://sub.example.com/index.php$1 [R=301,L]
    

    但这使得无法在 /sub/ 中创建其他文件,因为它总是将您重定向到 index.php

如果我还想在 /sub/ 中创建始终重定向到子域版本的其他文件,那么解决此问题的最佳方法是什么?

【问题讨论】:

  • 子目录结构是否与子域相同? IE。 http://sub.example.com/path/to/my/file.php会重定向到http://example.com/sub/path/to/my/file.php吗?
  • @bluemoon6790 应该是相反的,但是是的

标签: .htaccess redirect mod-rewrite


【解决方案1】:

位于您网站根目录的以下内容应将所有点击example.com/sub 的内容重定向到sub.example.com

RewriteEngine On 
RewriteRule ^sub/(.*)$ http://sub.example.com/$1 [R=301,NC,L]

如果您需要调整这些规则,可以在 https://htaccess.madewithlove.be/ 对其进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    相关资源
    最近更新 更多