【问题标题】:htaccess: subdomain to folder on server. Keep URLhtaccess:子域到服务器上的文件夹。保留网址
【发布时间】:2016-03-31 01:18:41
【问题描述】:

我需要帮助(内部重定向?)使用 apache 的 htaccess。

在我的服务器上,我有一个 主域

url:domain.com
服务器路径:/opt/public_html/domain.com/

和一个子域

url:sub.domain.com
服务器路径:/opt/public_html/sub.domain.com/

当访问者键入 sub.domain.com 时,我希望 URL 保持不变。我还希望服务器将文件/opt/public_html/domain.com/index.php 提供给访问者。我如何在 .htaccess 中执行此操作?

此外,如果访问者键入 sub.domain.com/sofa/,服务器应在不更改 URL 的情况下提供文件 /opt/public_html/domain.com/sofa/index.php

明白了吗?好的最后一个例子:

如果访问者键入 sub.domain.com/?big=mac,服务器应显示文件 /opt/public_html/domain.com/?big=mac,而不更改 URL。

任何想法如何做到这一点?

【问题讨论】:

  • 对不起?所以访问http://sub.domain.com 转到/public_html/sub.domain.com/,而http://sub.domain.com/sofa 转到/public_html/domain.com/sofa/index.php?这很奇怪!
  • 没有。如果http://sub.domain.com 然后/public_html/domain.com/ 如果http://sub.domain.com/sofa 然后/public_html/domain.com/sofa/index.php(或/public_html/domain.com/sofa/ 无关紧要)
  • 但是在你的问题中你没有那样说?
  • 没有。现在sub.domain.com 转到/public_html/sub.domain.com/ 而我希望sub.domain.com 转到/public_html/domain.com/

标签: php apache .htaccess redirect


【解决方案1】:

如果您在 .htaccess 中启用 RewriteEngine,您应该能够创建一个条件和规则来处理在子域 URL 处提供主域的页面。像这样的:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} sub.domain.com$
RewriteCond %{REQUEST_URI} !^/sub.domain.com
RewriteRule .* domain.com/%1 [L] 

RewriteCond 行使规则适用于 sub.domain.com 上的任何页面,并且应该从 public_html/sub.domain.com 提供服务。它应该(希望在内部)将 URI 更改为 http://domain.com/whatever,而不在浏览器中进行更改。

查看this questionthis one 了解类似信息。如果 .htaccess 真的不适合您,您可以尝试代理 mod_rewrite,或创建符号链接。

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多