【问题标题】:Redirect to all html page and folders to index.html重定向到所有 html 页面和文件夹到 index.html
【发布时间】:2015-07-11 12:34:14
【问题描述】:

如何将所有文件夹重定向到我的 index.html 并确定用户来自哪里。

首先我只有index.html.htaccess 文件。

场景:

  1. 用户点击链接:mydomain.com/blabla(通常转到 404)
  2. 网站重定向到用户mydomain.com/index.htmlmydomain.com/index.html?par=blabla
  3. mydomain.com/index.html 中显示警报“来自mydomain.com/blabla

这可能吗?谢谢。

更新**********

我找到了这样的解决方案。

首先更改.htaccess文件:https://stackoverflow.com/a/21663207/1173413

并使用 alert(window.location); 就像 Polostor 所说的那样。

【问题讨论】:

  • 是的,这是可能的,但你有没有尝试过?
  • 我在htaccess文件RewriteEngine on RewriteCond %{REQUEST_URI} !^/index.html$ RewriteRule .* /index.html [L,R=302]alert(document.referrer) index.html文件中写了这个
  • 我在想你可以有一个通用的 js 函数,在每个页面的页面加载时,通过传递当前页面的 url 来调用这个通用函数,并在那个通用函数中进行重定向。在重定向时,将重定向页面的 url 作为查询字符串传递并在 index.html 中准备好它
  • 问题是我没有 blabla 文件夹,文件夹名称可以由用户更改。我编辑了问题

标签: javascript jquery html


【解决方案1】:

我可以想象这样的事情。

文件 .htaccess 以重定向页面并将最后一页设置为 getter。当 .htaccess 在主文件夹中时,它适用于任何子文件夹。

罢工>

RewriteEngine on
RewriteRule ^(.*) /index.html?page=$1 [R=301,L]

编辑:更好的解决方案应该是https://stackoverflow.com/a/12500251/4745695

规则是

  1. R 代表重定向,响应代码为 301
  2. L 表示这是最后一条规则

index.html 中会是这样的。

<html>
  <head>
    <title>My title</title>
    <script>
      alert(window.location.search.substring(1));
    </script>
  </head>
  <body>
  </body>
</html>

我从How to get the value from the GET parameters?(分别为location.search in javascript)获取了get方法,并且.htaccess在http://www.jakpsatweb.cz/server/的那些页面上得到了很好的描述(在捷克语中,使用谷歌翻译器翻译它们)。

【讨论】:

  • 我尝试了您的 .htaccess 并添加了“/”字符:RewriteEngine on RewriteRule (.*) /index.html?page=$1 [R=301,L] 结果 url 为 example.com/index.html?page=index.html没有“blabla”文件夹名称
  • 和火狐说:“路由无效”
  • @qwerty 谢谢,忘记了两件事 - 在 (.*) 之前添加 ^,这应该对文件夹产生魔力,正如你所说的 / 在 index.html 之前 - 我在答案中更改了它。 . 而对于“路由无效”我从来没有见过,我什至不能谷歌很多..
  • "Routing Invaild" 是由谷歌从土耳其语翻译而来的 :) 你的旧代码进入循环。 example.com/blabla 调用 example.com/example.com/blabla 及其调用 example.com/example.com/blabla/exmaple.co.....
  • 对,对,对.. 我现在可以看到了.. 很抱歉.. 最好和最干净的解决方案是 IMO stackoverflow.com/a/12500251/4745695 用于 .htaccess
猜你喜欢
  • 2014-06-17
  • 2020-09-30
  • 1970-01-01
  • 2012-12-01
  • 2016-10-16
  • 1970-01-01
  • 2013-07-16
  • 1970-01-01
  • 2012-01-15
相关资源
最近更新 更多