javascript - http 或 www 到 https 重定向不起作用

标签 javascript jquery html .htaccess

我的网站应该将所有指向我网站的链接重定向到 https://域,但是当以某些方式键入时,它不起作用。

https://www.example.com ---作品

example.com --- 有效

www.example.com --- 不起作用,请转到 http://...

网站从单独的文件中调用全局页眉和页脚。当它不起作用时,它会抛出此错误:

    Access to XMLHttpRequest at 'https://www.example.com/header.html'
    from origin 'http://www.example.com' has been blocked by CORS policy:
    Response to preflight request doesn't pass access control check: The
    value of the 'Access-Control-Allow-Credentials' header in the response
    is '' which must be 'true' when the request's credentials mode is
    'include'. The credentials mode of requests initiated by the
    XMLHttpRequest is controlled by the withCredentials attribute.

这是调用头文件的代码:

    var headerURL = 'https://www.example.com/header.html';
    var xhr = new XMLHttpRequest();
    xhr.open('POST', headerURL, true);
    xhr.withCredentials = true;
    xhr.onreadystatechange = function()
    {
      if (xhr.readyState === 2)
      {
        $('header').load(headerURL + ' header', function()
        {
          ...do stuff with the header...
        });
      }
    }
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON);

这是我的 .htaccess 文件:

    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
    RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteRule ^(.*)$ $1.html

    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

    Redirect /page1.html /page1
    Redirect /page2.html /page2
    Redirect /page3.html /page3
    Redirect /page4.html /page4
    Redirect /page5.html /page5

    ErrorDocument 404 /404.shtml
    ErrorDocument 500 /500.shtml

    # Enable CORS
    Header always set Access-Control-Allow-Origin "https://www.example.com"
    Header always set Access-Control-Allow-Origin "http://www.example.com"

    # ----------------------------------------------------------------------
    # Expires headers (for better cache control)
    # ----------------------------------------------------------------------

    <IfModule mod_expires.c>
      ExpiresActive on

      ...Lots of ExpiresByType rules...

    </IfModule>

我对编写 .htaccess 文件一无所知。这只是我发现做了我需要它做的事情的部分(删除“.html”,重定向到自定义错误页面,并设置自定义过期时间,...)

最佳答案

尝试在 .htaccess 上执行以下操作:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

关于javascript - http 或 www 到 https 重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240198/

相关文章:

javascript - 如何避免大型 HTML 表格中的文本分页(打印/PDF)?

html - Firefox 中的 CSS 列数

html - CSS 正则表达式 id 选择器与类选择器的性能

javascript - 将全局变量传递给函数 - 函数运行后全局变量不更新

javascript - Angular .js : $sanitize fails on $injector:modulerr using exact code example from angular site

jquery - 在 iOS 或任何触摸设备上双击

javascript - 如何防止错误 : "Window navigated away"

javascript - 单击时使用 javascript 添加内容

单击按钮时显示 DIV 容器的 Javascript 函数

Javascript 多个 OR 在 if 条件中使用 for 循环