Encoding URLs

What is URL Encoding?

When you pass information through a URL, you need to make sure it only uses specific allowed characters like: alphabetic characters, numerals, and a few special characters that have meaning in the URL string. Any other characters should be encoded so that they don't cause problems.

The most commonly encoded character is the <space> character. You see this character whenever you see a plus-sign (+) in a URL. This represents the space character. The plus sign acts as a special character representing a space in a URL. The most common way you'll see this is in a mailto link that includes a subject. If you want the subject to have spaces in it, you can encode them as pluses:

mailto:email?subject=this+is+my+subject

How Do You Encode a URL?

Simply replace the special characters with their encoding string. This will nearly always begin with a %. If you don't want to do it by hand, you can use a script such as is found on the JavaScript site: Encoding Web Addresses.

When Should I Encode a URL?

Strictly speaking, you should always encode any special characters found in a URL. But you generally won't find any special characters in a URL outside their normal context except with form data. If you submit data to CGI scripts using the GET method, you should encode the data as it will be sent over the URL. For instance, if you are writing a link to promote an RSS feed, your URL will need to be encoded to add to the script URL you're promoting it on.

What Should be Encoded?

Any character that is not an alphabetic character, a number or a special character that is being used outside its normal context. Below is a table of characters and their encoding. Full URL Encoding Table

Reserved Characters URL Encoding

CharacterPurpose in URLEncoding
:Separate protocol (http) from address%3B
/Separate domain and directories%2F
#Separate anchors%23
?Separate query string%3F
&Separate query elements%24
@Separate username and password from domain%40
%Indicates an encoded character%25
+Indicates a space%2B
<space>Not recommended in URLs%20 or +
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值