创建Ajax的网站与多个子文件夹的链接失败多个、文件夹、链接、网站

2023-09-02 11:40:31 作者:简以时光

我试图创建一个网站是加载所有它的通过Ajax 内容。所以我们可以说该网站是 www.abc.net

I'm trying to create a site that is loading all it's content via Ajax. So let's say the site is www.abc.net.

我的 abc.net/index.html ,然后该文件将永远被调用,不管URL(文件夹/文件)中输入。所以如 abc.net/somesubsite 被调用时,URL应该保持原样,并且只调用中的index.html。

I have abc.net/index.html and this file shall always being called, whatever URL(folder/file) was typed in. So e.g. abc.net/somesubsite was called, the URL should stay as it is and just calling the index.html.

我们的想法是,现在看哪个URL被称为以及处理与jQuery和显示的具体股利。通过这种方式有着直接的联系将是容易做到。

The idea is now to look which URL was called and to handle that with jQuery and showing the specific div. In that way a direct link would be easily possible.

那么我的问题是,我不能让URL逗留,因为它是和其时的 abc.net/somesubsite/someother ,然后我得到一个服务器错误。我是玩弄htaccess,但它认为它不是一个真正的完美的解决方案。

Well my issue is that I'm not able to let the URL stay as it is and when having abc.net/somesubsite/someother then I get an server error. I was playing around with htaccess but it feels that it's not really a perfect solution.

有没有好办法做到这一点?它像本网站,但我希望能够处理的文件夹/子文件夹中这是不可能存在。我感兴趣的任何解决方案,这也是没有问题的,如果有真正需要的加载不同的HTML文件。但是,如果可能,我想有都在一个文件中。

Is there good way to do that? It is like this site but I want to be able to handle folder/subfolder which is not possible there. I'm interested in any solution, it's also no problem to have different html files that are loaded if really needed. But if possible I would like to have all in one file.

推荐答案

更​​新你的.htaccess文件如下所示:

Update your .htaccess file like so:

RewriteEngine On 
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

这将重定向所有相关的服务器调用的index.php URL =<其余网址的制造>

This will redirect all relevant server calls to index.php?url=<rest of the url>.

现在,重命名 index.html的的index.php (我们需要写一个小PHP code)。

Now, rename your index.html to index.php (we need to write a little php code).

在你的JavaScript写:

In your JavaScript write:

var url = "<?php echo $_GET['url']; ?>";

和你有它。现在,您可以分割字符串以/分隔符来找到你所有需要的组件。

And there you have it. You can now split the string by '/' delimiter to find all your needed components.

根据你的例子,以下网址: abc.net/somesubsite/someother 将重定向到的index.php 网​​址变量将持有 somesubsite / someother

As per your example, the following url: abc.net/somesubsite/someother will redirect to index.php and the url variable will hold somesubsite/someother.

您可以再使用 url.split('/')来获得所有子文件夹在数组中。

You can then use url.split('/') to get all "subfolders" in an array.

您还需要重写你的网站的所有链接的行为:

You will also have to override all links' behavior in your site:

prevent正常动作 装载Ajax内容 pushState更改URL

像这样:

$('a').on('click', function(e){ 
    e.preventDefault(); // 1
    var $url = this.href;
    loadContent($url); // 2 
    window.history.pushState("object or string", "Title", $url); // 3        
});