2013-08-18 36 views
0

所以我想在我的網站上創建一個搜索,我需要編碼一些文本,所以它是URL友好的。然而,如果我搜索任何帶有「<」符號的東西,我會得到HTTP錯誤403(禁止訪問),因爲「<」未被編碼。encodeURIComponent is failing

這是我使用的代碼:

var search = $("#txtHomeSearch").val(); 

if(search != ""){ 
    var urlSearch = encodeURIComponent(search); 
    window.location.href = "/search&s=" + urlSearch; 
} 

工作URL的例子:http://website.com/search&s=helloword
一個URL損壞的例子:http://website.com/search&s= <

也許問題是我的.htaccess文件其中包含:

RewriteEngine on 
RewriteRule ^([^.*]+)$ index.php?page=$1 [L] 
ErrorDocument 404 /errorPages/404.php 
+0

'<'不是網址中的特殊字符,所以它不需要編碼。 – Barmar

+0

當「<」在我的網址中時,我得到HTTP錯誤403,這就是爲什麼我需要對它進行編碼的原因 – JamesConnor

+0

您使用的服務器是什麼? – MasterAM

回答

0

這裏有一個簡單的實用程序:http://www.the-art-of-web.com/javascript/escape用於驗證各種Javascript轉義函數的操作。根據ECMA標準並使用該工具進行驗證,應通過encodeURIComponent()函數正確地轉義「<」。

難道是「<」以外的字符導致問題嗎?對於encodeURIComponent丟失的字符有各種補救措施。一個是此處和其他地方列出的url_encode功能:javascript window.location do I need to escape?

+0

沒有其他字符導致問題。這個工程...「http:// localhost/search&s = helloworld」,這不起作用「http:// localhost/search&s = <」 – JamesConnor

0

嘗試使用[B]標誌轉義您的後端引用。

RewriteEngine on 
RewriteRule ^([^.*]+)$ index.php?page=$1 [B,L] 
ErrorDocument 404 /errorPages/404.php