【问题标题】:Redirect subdirectories to root without changing URL将子目录重定向到根目录而不更改 URL
【发布时间】:2017-09-17 14:53:33
【问题描述】:

假设我的主站点文件所在的域为 www.example.com。出于跟踪目的,我有各种虚荣网址,例如 www.example.com/blah 或 www.example.com/blue、www.example.com/bleh,其中 blue、blah 和 bleh 只是 www.example 的空文件夹/子目录。 com.

我目前正在使用元标记重定向回 www.example.com(root),但这会丢失带有附加子目录的原始 URL。

如何在不丢失 URL 的情况下进行重定向?

实际上,这些虚 URL 看起来更像:www.example.com/bleh?id=1232,并且在 URL 中也显示该参数很重要。

【问题讨论】:

    标签: javascript html .htaccess redirect mod-rewrite


    【解决方案1】:

    如果您可以在每个子域中创建唯一的 html 页面,您可以在标签中硬编码 url:

    <meta http-equiv="refresh" content="0; url=http://www.example.com?param=bleh">
    

    如果您想使用 1 个文件并使用 URL 动态构建元重定向,您可以使用 javascript 将当前窗口位置放入重定向 url 的参数中。在你的 body 标签底部添加以下标签:

    <script>
      var metaTag = document.createElement('meta');
          metaTag.httpEquiv = "refresh";
          metaTag.content = "0; url=http://www.example.com?param="+encodeURI(window.location.href);
          document.getElementsByTagName('head')[0].appendChild(meta);
    </script>
    

    【讨论】:

    • 乐于助人。如果它回答了问题,请标记为已回答;)干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2015-06-22
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多