2016-09-14 87 views
1

我想從這個清理我的網址:.htaccess,如何從url中刪除模式?

https://example.com/track&id=180 

到:

https://example.com/track/180 

我的代碼如下:

.htaccess

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule^- [L] 
RewriteRule ^welcome/([^/]+)/?$    index.php?a=welcome&filter=$1 [NC,QSA,L] 
RewriteRule ^track/([^/]+)/?$     index.php?a=track&filter=$1  [NC,QSA,L] 
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3    [L,QSA] 

但是,如果我使用第二個示例(沒有&id=的URL)通過Ajax它返回「未定義」。

我嘗試Ajax調用此鏈接按鈕

<a href="'.$this->url.'/track/'.$id.'" rel="loadpage">Click Here</a> 

我認爲這是不確定的,因爲在我的請求,我得到的id參數在PHP作爲$_GET['id'];

所以我的問題是:爲什麼它不適用於Ajax?

這裏是我的functions.js

$(function() { 
    $("body").on("click", "a[rel='loadpage']", function(e) { 
     // Get the link location that was clicked 
     startLoadingBar(); 
     pageurl = $(this).attr('href'); 
     // Replace the path of the url from index to the path with raw content 
     var custom = pageurl; 
     var custom = custom.replace(baseUrl+'/track', 'page.php?a=track'); 
     var custom = custom.replace(baseUrl+'/', 'page.php?a=welcome'); 
     // Request the page 
     $.ajax({url:custom,success: function(data) { 
      // Show the content 
      $('#content').html(data); 
      // Stop the loading bar 
      stopLoadingBar(); 
      // Scroll the document at the top of the page 
      $(document).scrollTop(0); 
      // Reload functions 
      selectExplore(); 
      reload(); 
     }}); 


     // Store the url to the last page accessed 
     if(pageurl != window.location){ 
      window.history.pushState({path:pageurl}, '', pageurl); 
     } 
     return false; 
    }); 
}); 

.htaccess是不是我的專長,所以任何幫助,將不勝感激。

在此先感謝。

+0

這個問題只與阿賈克斯 – NineCattoRules

+0

請問,如果你只是把它在瀏覽器中重寫URL工作VS用ajax? –

+0

@PanamaJack是的,如果我直接去'https:// example.com/track/180',一切正常。 – NineCattoRules

回答

1

您可以使用此JS代碼發送您的AJAX調用:

var custom = pageurl.replace(baseUrl+'/track/', '/page.php?a=track&id='); 

// Request the page 
$.ajax({url:custom,success: function(data) { 
    // ... 
} 
+1

非常感謝你,我只是改變了這個:'var custom = pageurl.replace(baseUrl +'/ track /','page.php?a = track​​'以防萬一你不在根文件夾中。 – NineCattoRules