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 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效