你好,我很难做到这一点.htaccess将停止在ajax调用中重写url ..
我的.htaccess如下:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteRule ^([^/\.]+)/?$ index.php?system=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?system=$1&task=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?system=$1&task=$2&id=$3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?system=$1&task=$2&id=$3&data=$4&key=$5
和PHP ajax调用:
<script>
$(document).ready(function () {
$(".checkButton").click(function () {
var objID = this.id;
$.ajax({
url: "/update.php",
type: 'POST',
data: {val: objID},
success: function (data, textStatus, jqXHR)
{
alert("sucess" + data);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert(textStatus + errorThrown + jqXHR);
}
});
});
});
由于现有大量代码,我不想改变我们现有的重写规则,但是找到一种方法可以忽略只有ajax请求的重写规则。我一直在寻找一段时间,我只能找到使用
的建议RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
编辑:
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteRule ^([^/\.]+)/?$ index.php?system=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?system=$1&task=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?system=$1&task=$2&id=$3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?system=$1&task=$2&id=$3&data=$4&key=$5
修改后的.htaccess ajax在测试后仍无法找到该页面。
答案 0 :(得分:2)
RewriteCond
仅适用于之后的第一个RewriteRule
。
改为使用:
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{HTTP:X-Requested-With} =XMLHttpRequest
RewriteRule ^ - [L]
RewriteRule ^([^/.]+)/?$ index.php?system=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?system=$1&task=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ index.php?system=$1&task=$2&id=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ index.php?system=$1&task=$2&id=$3&data=$4&key=$5 [L]