Redirection for Ajax content

Hello There!

I’m building a portfolio website which essentially comprises of a ‘main’ page, then all ‘projects’ are brought in via Ajax requests.

I’m looking for the best way to redirect the user to the ‘main’ page, should they try to access a ‘project’ page directly, but in a way that the redirect does not effect the ajax request. So far I’ve tried using a combination of the go() method and the .filter('#id') / .find('#id') jQuery methods, then got a little lost with 301 redirects in the .htaccess.

Maybe it is a problem with syntax, more likely I have this completely wrong. If this is not the place for this question and I should trawl the jQuery boards, please feel free to send me packing! Any help would be hugely appreciated.

My code returns the ‘main’ page as the content as the redirect still seems to be in play. I would like to try to block the redirect unless the user attempts to access the page directly:

PHP:

<div id="requested-content">
  //content to be brought in
</div>   
<div id="redirect">
   <? go($site->url('main.php'), 301); ?>
</div>

JS:

function ajaxLink(){
  $('.project-link').click(function(event){
    var projectURL = $(this).attr('href');
    event.preventDefault();
    $.ajax({
          url: thisURL, 
          success: function(response) {
          result = $(response).find('#requested-content');
          $('#element-recieving-content').html(response);
      }
  });

You can check if the request is an Ajax request or not and react on that:

if(kirby()->request()->ajax()) {
  // do something
} else {
  // do something else
}

Alternatively, you can use content representations.

Arghh! Works a treat, thank you!