1

所以我试图在我的网站中创建一个搜索,我需要对一些文本进行编码,以便它对 URL 友好。但是,如果我用“<”符号搜索任何内容,我会收到 HTTP 错误 403(禁止访问),因为“<”没有被编码。

这是我正在使用的代码:

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
4

2 回答 2

0

There is a simple utility here: http://www.the-art-of-web.com/javascript/escape for verifying the operation of the various Javascript escaping functions. Accoring to the ECMA standard, and verified using that tool, the "<" should be escaped correctly by the encodeURIComponent() function.

Could it be a character other than "<" causing the problem? There are various remedies for the characters that encodeURIComponent misses. One is the url_encode function listed here and elsewhere: javascript window.location do I need to escape?

于 2013-08-18T01:35:05.510 回答
0

尝试使用[B]标志转义您的反向引用。

RewriteEngine on
RewriteRule ^([^.*]+)$ index.php?page=$1 [B,L]
ErrorDocument 404 /errorPages/404.php
于 2013-08-18T03:00:41.160 回答