首页 > 解决方案 > Ajax 和 htaccess - 仅重定向内容 div,复制整个模板

问题描述

我想在我的页面中使用音频播放器,但是当页面刷新时播放器无法重新加载。所以,我将模板分为 3 个 div:页眉、内容和页脚,即播放器所在的位置。要重定向到另一个页面,我使用 ajax 仅刷新内容 div。同时,我使用 htaccess 将所有内容重定向到我的基本模板 index.php。我这样做是为了始终验证每个请求,在存在时显示请求的页面,或者在不存在时显示 404 页面。问题是当我重定向到任何页面时,例如“/about”。因为是htaccess,所以index.php 是要到内容div 的。我只想在 index.php 中加载我的基本模板一次。我的场景是否有解决方法。我不想使用来自客户端的任何信息,例如标头。只是出于安全原因。下面,有'

我的 index.php

<?php

//Creating base template...;

//Here, will be a URI validation
$uri = $_SERVER["REQUEST_URI"];

?>

<script>

    $(document).on('click', "a", function(e){

        $.ajax({
            async: false,
            url: document.location.pathname,
            type: 'post',
            success: function(ret) {

                $("#content").html(ret);

            }
        });

        return false;
    });

</script>

<div id="header">
    <a href="/home">Home</a>
    <a href="/about">About</a>
</div>

<div id="content">
    <h1>Content Here</h1>
</div>

<div id="footer">
    <audio controls>
      <source src="horse.ogg" type="audio/ogg">
      <source src="horse.mp3" type="audio/mpeg">
    Your browser does not support the audio element.
    </audio>
</div>

我的 htaccess

RewriteEngine On

DirectoryIndex index.php

RewriteRule ^(.*)$ index.php [L]

我的 about.html 页面

</h1>About Page</h1>

标签: phpajax.htaccessredirect

解决方案


推荐阅读