<p>Sure, you can do something like</p>

<pre><code>{% if not craft.request.isAjax() %}
  {% redirect siteUrl %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-05-22T19:53:53.227Z
2015-05-22T19:53:53.227Z
Alec Ritson
<p>So doing some entries loading via ajax from this question which I expanded on.
<a href="https://craftcms.stackexchange.com/a/9868/3579">https://craftcms.stackexchange.com/a/9868/3579</a></p>

<p>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 </p>

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

<p>so I setup a route with the following.</p>

<p>URI resource-center-ajax</p>

<p>Template /resource-center/_resource-center-ajax</p>

<p>Actual Template file _resource-center-ajax.html</p>

<p>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.</p>

<p>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?</p>

<templating><routes><ajax><pagination>
Using ajax with routes is it right?
# Using ajax with routes is it right?

(Asked by: username_0 on 2015-05-22)

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?

**Tags:** templating, routes, ajax, pagination

---

## 1 answers

### Answer by username_1 on 2015-05-22. Score: 4

Sure, you can do something like
{% if not craft.request.isAjax() %}
  {% redirect siteUrl %}
{% endif %}


---

