So doing some entries loading via ajax from this question which I expanded on. https://craftcms.stackexchange.com/a/9868/3579

I'm curious about setting up a route for the actual ajax file since it seems a route is needed otherwise it doesn't work, and will 404. In my example you can see I am setting the ajax url as

$.get( "/resource-center-ajax/p"+page, function( data ) {//do stuff}

so I setup a route with the following.

URI resource-center-ajax

Template /resource-center/_resource-center-ajax

Actual Template file _resource-center-ajax.html

If I go to example.com/resource-center-ajax it will show the results from my ajax file which is just a plain page since I am not including the layout file in that template file.

Is there a way for me to prevent people from accessing this url directly and make it only used for the ajax request? Maybe check if its an ajax request if not redirect to the homepage or something?

1 Answer 1


Sure, you can do something like

{% if not craft.request.isAjax() %}
  {% redirect siteUrl %}
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.