URL重写而不影响ajax函数

问题描述 投票:3回答:2

我正在制作分类广告网站。我需要在没有网址参数的情况下加载一个广告。我成功地使用了URL参数。现在我想从URL中删除参数。我存储了URL-slug,它构建了在数据库中节省广告时间。在广告页面加载时,有许多函数被调用。这些函数根据URL参数值从数据库中请求数据。我尝试了htaccess规则。但其余的页面和细节都像以前一样加载。但ajax函数称为数据,应该显示在div标签上不起作用。他们在这些div中显示整个页面。

到目前为止,我尝试过遵循htaccess规则。

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

现在我将展示我的功能样本。

  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;

    }

在ad.php文件的顶部,我包含这些代码行来获取url的最后一部分并将其存储在变量中。

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

同样在JavaScript函数中,我通过ad_id将其检索到ajax,获取当前查看广告页面的相关数据。

在广告列表页面中,我展示了带有“网址”的广告。这可以通过以下方式完成,

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

更多=>当我看到来自chrome开发工具的网络响应时,它显示无限加载相同的css,js,其他外部库文件和ajax函数,称为响应递归。它还将id参数添加到ajax URL的末尾。

在ad.php文件的顶部,我回显$unique_ad_url以查看值。它还显示了所有div中的内容,这些div应该只显示检索到的数据。正如我上面提到的整个广告页面,它显示了回声值。

最后,我想加载ajax检索到的值,以便在广告页面上显示,而不会出现此类问题。和URL预计会像,http://ads.com/ad/daihatsu-boon-m700s-new-2016-2019-04-05-22-49-04我感谢你对这个问题的帮助。

.htaccess url url-rewriting
2个回答
1
投票

在你的.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]

1
投票

嘿,我也遇到了这个问题。您可以从中找到帮助。

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
© www.soinside.com 2019 - 2024. All rights reserved.