通过.htaccess重定向AJAX查询

问题描述:

我有一个.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]

这将重定向所有查询并设置干净的URL.但是,当我使用ajax时,它也会重定向.是否可以过滤Ajax查询并重定向到

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]

相反?

我试图在controller.php中捕获它,但是 X_REQUESTED_WITH 不存在.最好的办法是将所有ajax请求重定向到另一个脚本,而不进行其他脚本检查.

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.

调用AJAX时,将GET变量添加到查询中(即在查询末尾添加?ajax = true ).然后在您的.htaccess文件中添加该文件:

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]