li x
li x

Reputation: 4051

.htaccess won't stop rewriting when ajax request is made

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.

Upvotes: 1

Views: 873

Answers (1)

Croises
Croises

Reputation: 18671

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]

Upvotes: 2

Related Questions