简体   繁体   English

减小URL大小的方法?

[英]Methods of reducing URL size?

So, we have a very large and complex website that requires a lot of state information to be placed in the URL. 因此,我们有一个非常大而复杂的网站,需要将许多状态信息放置在URL中。 Most of the time, this is just peachy and the app works well. 在大多数情况下,这只是桃红色,应用程序运行良好。 However, there are (an increasing number of) instances where the URL length gets reaaaaallllly long. 但是,在(越来越多的)实例中,URL长度变得非常长。 This causes huge problems in IE because of the URL length restriction. 由于URL长度限制,这会在IE中引起巨大问题。

I'm wondering, what strategies/methods have people used to reduce the length of their URLs? 我想知道,人们使用了哪些策略/方法来减少其URL的长度? Specifically, I'd just need to reduce certain parameters in the URL, maybe not the entire thing. 具体来说,我只需要减少URL中的某些参数,也许不是全部。

In the past, we've pushed some of this state data into session... however this decreases addressability in our application (which is really important). 过去,我们将一些状态数据推入了会话中……但是,这降低了应用程序的可寻址性(这非常重要)。 So, any strategy which can maintain addressability would be favored. 因此,任何可以保持可寻址性的策略都将受到青睐。

Thanks! 谢谢!

Edit: To answer some questions and clarify a little, most of our parameters aren't an issue... however some of them are dynamically generated with the possibility of being very long. 编辑:要回答一些问题并进行一些澄清,我们的大多数参数都不是问题...但是其中一些参数是动态生成的,可能会很长。 These parameters can contain anything legal in a URL (meaning they aren't just numbers or just letters, could be anything). 这些参数可以包含URL中合法的任何内容(这意味着它们不仅是数字或字母,还可以是任何东西)。 Case sensitivity may or may not matter. 区分大小写可能无关紧要。

Also, ideally we could convert these to POST, however due to the immense architectural changes required for that, I don't think that is really possible. 同样,理想情况下,我们可以将它们转换为POST,但是由于为此需要进行巨大的体系结构更改,我认为这实际上是不可能的。

If you don't want to store that data in the session scope, you can: 如果您不想在会话范围内存储该数据,则可以:

  • Send the data as a POST parameter (in a hidden field), so data will be sent in the HTTP request body instead of the URL 将数据作为POST参数发送(在隐藏字段中),因此数据将在HTTP请求正文中而不是URL中发送
  • Store the data in a database and pass a key (that gives you access to the corresponding database record) back and forth, which opens a lot of scalability and maybe security issues. 将数据存储在数据库中并来回传递密钥(使您可以访问相应的数据库记录),这带来了很多可伸缩性,甚至可能带来安全性问题。 I suppose this approach is similar to use the session scope. 我想这种方法类似于使用会话范围。

most of our parameters aren't an issue... however some of them are dynamically generated with the possibility of being very long 我们的大多数参数都不是问题...但是其中一些是动态生成的,可能会很长

I don't see a way to get around this if you want to keep full state info in the URL without resorting to storing data in the session, or permanently on server side. 如果您想在URL中保留完整的状态信息而又不求助于将数据存储在会话中或永久存储在服务器端,则我看不到解决此问题的方法。

You might save a few bytes using some compression algorithm, but it will make the URLs unreadable, most algorithms are not very efficient on small strings, and compressing does not produce predictable results. 可能会使用某些压缩算法来节省一些字节,但是这会使URL无法读取,大多数算法对小的字符串效率不高,并且压缩不会产生可预测的结果。

The only other ideas that come to mind are 想到的唯一其他想法是

  • Shortening parameter names ( query => q , page => p ...) might save a few bytes 缩短参数名称( query => qpage => p ...)可能节省一些字节

  • If the parameter order is very static, using mod_rewritten directory structures /url/param1/param2/param3 may save a few bytes because you don't need to use parameter names 如果参数顺序非常静态,则使用mod_rewrite目录结构/url/param1/param2/param3可以节省一些字节,因为您无需使用参数名称

  • Whatever data is repetitive and can be "shortened" back into numeric IDs or shorter identifiers (like place names of company branches, product names, ...) keep in an internal, global, permanent lookup table ( London => 1 , Paris => 2 ...) 无论数据如何重复,都可以将其“缩短”回数字ID或较短的标识符(例如公司分支机构的地名,产品名称等),并保存在内部,全局,永久的查找表中( London => 1Paris = > 2 ...)

Other than that, I think storing data on server side, identified by a random key as @Guido already suggests, is the only real way. 除此之外,我认为将数据存储在服务器端,这是唯一的真实方法,就像@Guido已经暗示的那样,用随机密钥标识数据。 The up side is that you have no size limit at all: An URL like 有利的一面是您完全没有大小限制:URL之类的

example.com/?key=A23H7230sJFC

can "contain" as much information on server side as you want. 可以根据需要在服务器端“包含”尽可能多的信息。

The down side, of course, is that in order for these URLs to work reliably, you'll have to keep the data on your server indefinitely. 当然,不利的一面是,为了使这些URL可靠运行,您必须无限期地将数据保留在服务器上。 It's like having your own little URL shortening service... Whether that is an attractive option, will depend on the overall situation. 就像拥有您自己的小型URL缩短服务...是否是有吸引力的选择,将取决于整体情况。

I think that's pretty much it! 我觉得差不多了!

One option which is good when they really are navigatable parameters is to work these parameters into the first section of the URL eg 当它们确实是可导航参数时,一个不错的选择是将这些参数放入URL的第一部分,例如

http://example.site.com/ViewPerson.xx?PersonID=123 => http://example.site.com/View/Person/123/ http://example.site.com/ViewPerson.xx?PersonID=123 => http://example.site.com/View/Person/123/

If the data in the URL is automatically generated can't you just generate it again when needed? 如果URL中的数据是自动生成的,您是否可以仅在需要时再次生成它?

With little information it is hard to think of a solution but I'd start by researching what RESTful architectures do in terms of using hypermedia (ie links) to keep state. 很少的信息很难想到一个解决方案,但我将从研究RESTful架构在使用超媒体(即链接)保持状态方面所做的事情开始。 REST in Practice ( http://tinyurl.com/287r6wk ) is a very good book on this very topic. REST实践http://tinyurl.com/287r6wk )是关于这一主题的非常好的书。

Not sure what application you are using. 不知道您正在使用什么应用程序。 I have had the same problem and I use a couple of solutions (ASP.NET): 我遇到了同样的问题,并且使用了几种解决方案(ASP.NET):

  1. Use Server.Transfer and HttpContext (PreviousPage in .Net 2+) to get access to a public property of the source page which holds the data. 使用Server.Transfer和HttpContext(.Net 2+中的PreviousPage)可以访问包含数据的源页面的公共属性。
  2. Use Server.Transfer along with a hidden field in the source page. 将Server.Transfer与源页面中的隐藏字段一起使用。
  3. Using compression on querystring. 在querystring上使用压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM