dongwuxie5112 2016-06-04 22:23
浏览 57
已采纳

发出ajax请求时,.htaccess不会停止重写

Hello I'm having great difficulty making it so .htaccess will stop rewriting the url in a ajax call..

my .htaccess below:

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

and the PHP ajax call:

<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);


 }
        });
    });
});

Because of an existing large amount of code I don't want to change the rewrite rules we have in place already but find a way in which I can ignore the rewrite rules for only a ajax request. I have been searching around for a while and I only managed to find suggestions of using

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest

edit:

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

Revised .htaccess ajax is still unable to find the page after testing.

  • 写回答

1条回答 默认 最新

  • drnf593779 2016-06-05 00:18
    关注

    RewriteCond works just for the first RewriteRule just after.

    Use instead:

    # 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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题