mavili
mavili

Reputation: 3424

Prevent htaccess from affecting ajax file requests

I have some rewrite rules in my .htaccess file for clean URLs, and I also need AJAX calls in my web pages. I have problems with ajax file requests because of the rewrite rules. Here is my htaccess file content:

Options -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteBase /~mavili/loran/orders/
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

When I do something like this:

$.ajax({
    type: 'get',
    url: 'http://domain.com/misc/page.php',
    success: function(data) {
        $('#load_content').html(data);
    },
    error: function() {
       $('#load_content').html('Error: Unable to load file.');
    }
});

the request for http://domain.com/misc/page.php goes through the htaccess rewrite rules and messes everything. Is there any way to prevent AJAX calls from going through htaccess settings?

Upvotes: 1

Views: 2961

Answers (1)

anubhava
anubhava

Reputation: 785196

One way of making Ajax request skip the rules is sticking a dummy query parameter in the URL e.g. this:

$.ajax({
    type: 'get',
    url: 'http://domain.com/misc/page.php&skip=1',
    success: function(data) {
        $('#load_content').html(data);
    },
    error: function() {
       $('#load_content').html('Error: Unable to load file.');
    }
});

And have your rewrite rules like this to skip all requests with ?skip=1 query parameter:

Options -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteCond %{QUERY_STRING} !^skip=1$
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

Upvotes: 5

Related Questions