prettyPhoto利用主题标签,但如果它们被编码(到 %23),大多数浏览器将出现 404 错误。这has been discussed before :
You get a 404 error because the #callback part is not part of the URL. It's a bookmark that is used by the browser, and it's never sent in the request to the server. If you encode the hash, it becomes part of the file name instead.
为什么仅仅因为哈希是 URI 编码的,它就会成为文件的一部分?不是bug吗?
我问是因为 prettyPhoto使用主题标签并遇到同样的问题。我想加一个'?在散列是最优雅的解决方案之前,我只是有点不知如何在现有代码中做到这一点:
function getHashtag(){
url=location.href;
hashtag=url.indexOf('#gallery')!==-1)?decodeURI(url.substring(url.indexOf('#gallery')+1,url.length)):false;
return hashtag;
}
function setHashtag(){
if(typeof theRel=='undefined')return; location.hash=theRel+'/'+rel_index+'/';
}
function clearHashtag(){
if(location.href.indexOf('#gallery')!==-1)location.hash="";
}Any other suggestions? I'll look into tweaking my 404 page, but that seems more like handling a problem rather than preventing it.
Thanks!
EDIT: Since evidently there's nothing wrong with the way prettyphoto handles those hashes, I ended up adding these rules to my apache server:
RewriteRule ^(.*).shtml(%23|#)$ /$1.shtml [R=301,NE,L]
RewriteRule ^(.*).shtml([^g]+)gallery(.+)$ /$1.shtml#gallery$3 [R=301,NE,L]
他们成功处理了 %23 导致问题的案例。
最佳答案
- Why would a hash become part of the file just because it's URI-encoded? Isn't it a bug?
如果您将浏览器指向 http://example.com/index.html#title ,浏览器将其解释为请求文件 index.html来自服务器example.com .请求完成后,浏览器会在文档中查找名称为“title”(即 <a name="title">My title</a> )的 anchor 元素。
如果您改为指向 http://example.com/index.html%23title , 浏览器请求文件 index.html%23title来自 example.com ,它可能在服务器上不存在,给你一个 404。看到区别了吗?
这不是错误。它是互联网标准的一部分 last updated 1998 年。参见 RFC 2396 .引用:
The character "#" is excluded because it is used to delimit a URI from a fragment identifier in URI references (Section 4).
至于 2 和 3,您的示例代码中没有足够的上下文来说明您要做什么。你如何调用你的代码?你想用不起作用的 prettyphoto 做什么?您是否尝试从用户点击或其他 javascript 事件重定向到特定照片或图库?您是否尝试在有人访问特定页面时打开图库?
我用 twitter/oauth 检查了链接的问题,但我看不出它与您提供的代码有何关系。我也开始研究 prettyphoto,但我也看不出您的代码与它有什么关系。
与其更改 404 页面,也许您需要的是一个代码内处理程序或服务器重写规则,它接受带有 %23 的未找到请求。在其中并将用户重定向到解码的 url。这可能有一些缺点,但如果您从无法控制的其他来源获取传入请求,那将是相当优雅的。你的服务器环境是什么? (语言、服务器技术、谁拥有机器等)
我很乐意为您提供解决方案或解决方法来更新我的答案。
关于javascript - 为什么URI-encoded ('#' ) anchors会导致404,JS中如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11587744/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun