Jacob88
Jacob88

Reputation: 85

Redirect AJAX query by .htaccess

I have a .htaccess file:

RewriteEngine On
DirectoryIndex control.php
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) controller.php?do=$1 [L]

This redirects all queries and sets up clean URLs. But when I use ajax it redirects that too. Is it possible to filter ajax queries and redirect to

controller-ajax.php?do=$1 [L]

instead?

I was trying to catch it in controller.php, but X_REQUESTED_WITH doesn't exist. The best thing then would be to redirect to another script all ajax requests, and not make an additional script check.

Upvotes: 0

Views: 2186

Answers (1)

Ansari
Ansari

Reputation: 8218

When you call AJAX, add a GET variable to the query (i.e. add ?ajax=true to the end of the query). Then have this in your .htaccess:

RewriteEngine On
DirectoryIndex control.php
Options +FollowSymlinks
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f {OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ajax.true
RewriteRule (.+) controller-ajax.php?do=$1 [L]

RewriteRule (.+) controller.php?do=$1 [L]

Upvotes: 1

Related Questions