sashiksu
sashiksu

Reputation: 354

URL re-writing without affecting ajax functions

I am making classifieds ad web site. In that I need to load one ad without URL parameters. I did it with URL parameters successfully. Now I want to remove parameters from the URL. I stored URL-slug which is constructed saving time of ad in database. In ad page load time there are many functions called. Those functions asks data from database according to URL parameter value. I tried with htaccess rules. But rest of page and details load as before. But ajax functions called data which are supposed to display on div tags not working. They show whole page inside those divs.

I tried following htaccess rules so far.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

ReWriteRule ^ad/([a-zA-Z0-9-/]+)$ ad.php?url=$1
ReWriteRule ^ad/([a-zA-Z0-9-/]+)/ ad.php?url=$1

Now I will show sample of my function.

  function load_reviews(){
        var id = "<?php echo $ad->get_ad_id($unique_ad_url) ?>";
        $.ajax({
            url: './middle/review/load_reviews',
            cache:true,
            type: 'GET',
            async: false,
            data:{
                id: id
            },
            success: function(response){
                $('#chat_area').html(response);
            },
            error: function (x, e) {
                if (x.status == 0) {
                    console.log('You are offline!! -  Please Check Your Network.');
                } else if (x.status == 404) {
                    console.log('Requested URL not found.');
                } else if (x.status == 500) {
                    console.log('Internal Server Error.');
                } else if (e == 'parsererror') {
                    console.log('Error. - Parsing JSON Request failed.');
                } else if (e == 'timeout') {
                    console.log('Request Time out.');
                } else {
                    console.log('Unknown Error. - ' + x.responseText);
                }
            }
        });
        return false;

    }

In the top of ad.php file I include these code lines to grab last part of url and store it in variable.

$url=$_SERVER['REQUEST_URI'];
//echo $url;
$unique_ad_url = basename(parse_url($url, PHP_URL_PATH));

Likewise in JavaScript functions I passed ad_id to retrieve it to ajax get relevant data for current viewing ad page.

In ad list page I showed ads with their' url. That is done by as below,

<a href="ad/'.$this->url_slug($ad['AD_URL']).'" class="card all-card">

More => When I see network responses from chrome develop tools it shows infinitely loading same css, js, other external library files and ajax function called responses recursively. Also it added id parameter to end of ajax URLs.

In the top of ad.php file I echo $unique_ad_url to see value. It also shows inside all divs which are supposed to show only retrieved data. As I mentioned above along with whole ad page it shows that echo value.

At the end I want load ajax retrieved values to present on ad page without such problems. And URL is expected to be like, http://ads.com/ad/daihatsu-boon-m700s-new-2016-2019-04-05-22-49-04 I appreciate your help for this issue.

Upvotes: 4

Views: 45

Answers (2)

anubhava
anubhava

Reputation: 784998

Try these rule in your .htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule ^ad/([\w-]+)/?$ ad.php?url=$1 [L,NC,QSA]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Upvotes: 1

Marketing Officer
Marketing Officer

Reputation: 31

hey I also faced that problem. You might find help from this.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

ReWriteRule ^ad/([a-zA-Z0-9-/]+)$ ad.php?url=$1
ReWriteRule ^ad/([a-zA-Z0-9-/]+)/ ad.php?url=$1

Upvotes: 1

Related Questions