0

Lets say i have website example.com, index page of which is made like header-main_div-footer. Using ajax I have links content load into specific div as well as changing pages URL. However, reloading page will only show content of included page without header and footer, s it supposed to be.

What i am looking for is when i manualy go to www.example.com/link/ it will load my websites index page (or tempplate) and put content of requested page (located at /link/) into specific div.

As for now, i have this script:

$(function(){
$('a[rel="tab"]').click(function(e){
    e.preventDefault(); 

    pageurl = $(this).attr('href');

    $.ajax({url:pageurl+'?rel=tab',success: function(data){
        $('#content').html(data);
    }});

    if(pageurl!=window.location){
        window.history.pushState({path:pageurl},'',pageurl);    
    }
    return false;  
});
});

I have no ideas on how to do it :(

lolbas
  • 794
  • 1
  • 9
  • 34

1 Answers1

0

The common name for this task is routing, and the answer you're looking for depends on the server you are using.

  • If you are running on Apache then you can add some rewrite directives to your .htaccess file. Take a look at the mod_rewrite module and any of the many places that talk about rerouting like: Rerouting all php requests through index.php
  • If you are running on php's development server, it's even easier! Just pass your router script to the server on start up: $ php -S localhost:8000 router.php See example 3 on this page in the manual.
  • I haven't tried routing on IIS, but I would probably start here: How to Implement URL Routing with PHP + IIS?
Community
  • 1
  • 1
Jwashton
  • 302
  • 1
  • 11
  • D: i thought it would be ajax-only – lolbas Jul 11 '14 at 13:17
  • @lolbas There will likely be some Ajax involved, especially since you're planning to load the page content that way. However, the sever needs to be able to send you to that Ajax in the first place, which will require a little server-side scripting. – Jwashton Jul 11 '14 at 17:18
  • btw, is there a way to use ajax to load non-requested page? so it looks like i go to www.example.com/about/ and it will also load page from www.example.com/index.php – lolbas Jul 11 '14 at 20:00
  • @lolbas Sure but, unless you plan to hardcode the file you want to load, you will need to have access to the url that the user requested. So say you set the server to always send you to index.php. In that index.php you can echo `$_SERVER["REQUEST_URI"]` into the Javascript, and parse it from there. You should be able to make whatever decisions you are wanting in Javascript and make the Ajax request as normal. However, I would generally recommend against using Ajax calls for entire pages, especially if the page is going to be fairly large, since you would be making 2 HTTP requests for each page – Jwashton Jul 12 '14 at 00:09