douqu2481 2017-01-04 02:12
浏览 37

使用JavaScript将子域重定向到域

I am using Apache envvars for creating the MYDOMAIN and MYSUBDOMAIN variables where I define 'mydomain.com' and 'sub.mydomain.com'. I then use these variables in the apache sites-available conf files to deploy the website. The 'sub' subdomain resides in /var/www/html/sub/. I need to have a JavaScript function on the subdomain website check whether or not a particular file exists in /var/www/html and if so, redirect to that location (i.e. mydomain.com). I cannot hard-code any specific URL's - these will be dealt with strictly using apache envvars, so I need a solution that will work regardless what the domain/subdomain is.

For this purpose, I am using AJAX like this, if file.txt returns a success, redirect to the /index.php:-

$(document).ready(function() {

            $.ajax({
                type: 'HEAD',
                url: '/file.txt',
                success: function() {
                            setInterval(function () {
                                    location.replace("/index.php");
                            }, 5000);
                },
                error: function() {
                            console.log('404 Not Found');
                }
            });
});

Unfortunately the document root for the subdomain is /var/www/html/sub therefore /file.txt and /index.php are effectively being looked for in /var/www/html/sub/ - I need this to look for file.txt in /var/www/html/ and then redirect to /var/www/html/index.php.

I also tried using '../file.txt' and '../index.php' but this returns the same result (i.e. '404 Error' is logged because it is looking for what is essentially /var/www/html/sub/file.txt). Since I cannot access the apache envvars from JS (I think - would be a major security scare if I could!!) - what other options have I got?

Thanks!

  • 写回答

1条回答

  • dounan4479 2017-01-04 02:59
    关注

    var hostFileUrl = function(fileName){
      return window.location.protocol + "://" + window.location.hostname + "/" + fileName;
    };
    
    $(document).ready(function() {
      $.ajax({
        type: 'HEAD',
        url: hostFileUrl('file.txt'),
        success: function(){
          setInterval(function(){
            window.location.assign(hostFileUrl("index.php"));
          }, 5000);
        },
        error: function(){
          console.log('404 Not Found');
        }
      });
    });

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64