You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何重定向子域名直接访问,仅允许通过指定URL访问

子域名访问控制与重定向需求及尝试方案

我拥有主域名 mydomain 和子域名 sub.mydomain,具体需求如下:

  • 直接访问 sub.mydomain 的请求需重定向至主域名 mydomain
  • 允许用户通过 sub.mydomain/url 路径正常访问

以下是我尝试过的两种解决方案:

方案一:PHP代码实现

// 设置会话标记
$_SESSION['fromMain'] = "true";
header("Location: noaccess.php");
if($_SESSION['fromMain'] == "false"){
   // 重定向回指定页面
   header("Location: foo.php");
}
else{
   // 重置会话标记
   $_SESSION['fromMain'] = "false";
}

方案二:Apache Rewrite规则实现

RewriteEngine On

# 仅允许来自特定URL特定目录的访问请求
SetEnvIf Referer "http://specificurl\.com/specificdirectory" ref_allowed
Order Deny,Allow
Deny from all
Allow from env=ref_allowed

# 将非特定来源的请求重定向至主域名
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://maindomain\.com/ [NC]
RewriteRule ^ http://maindomain.com [L,R=301]

内容的提问来源于stack exchange,提问作者it's Manon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 12:21:03