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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现