2010-08-04 29 views
4

我已经看到了一些相反的例子,但我正在寻找从锚点/哈希URL去一个非锚URL,像这样:如何使用mod_rewrite/htaccess从URL中检测并重定向?

From: http://old.swfaddress-site.com/#/page/name 
To: http://new.html-site.com/page/name 

http://karoshiethos.com/2008/07/25/handling-urlencoded-swfaddress-links-with-mod_rewrite/的例子无为我运作。这听起来像REQUEST_URI其中有/#/stuff,但我和我的Apache(2.0.54)都没有看到它。

任何想法,过去的经验或成功?

+0

该页面正在讨论如何解决Apple产品中错误编码'#'的错误,以致意外传递给服务器。正如Wrikken所说,这种情况不会发生,因此您无法阅读该片段。 – bobince 2010-08-04 20:54:16

回答

12

#后面的任何内容都是一个片段,并且将而不是发送到网络服务器。你无法在任何地方捕捉它,你必须使用客户端方法来捕捉它们。

1

我是您链接到的帖子的作者。 Wrikken是正确的,指定锚点后面的内容不会发送到服务器,除非在整个过程中一些URL已经被破坏。在客户端,你需要一些JavaScript这样在您的着陆页的链接swfaddress重定向到另一个域相应的URL:

var re = new RegExp('#(.*)'); 
var redirectFragment = re.exec(document.location.toString()); 
if (redirectFragment!=null) 
{ 
    document.location = 'http://new.html-site.com'+redirectFragment[1]; 
} 
2

@RobRuchte:岂不是更好地使用window.location.hash,以取代代替正则表达式?

var redirectFragment = window.location.hash.replace(/^#/,''); 
if ('' !== redirectFragment) { 
    window.location = 'http://new.html-site.com' + redirectFragment; 
} 
0

我使用@ m14t的答案的修改版本。这适用于重定向,看起来像http://example.com/path/to/page#fragment- >http://example.com/path/to/page/片段。请注意,我还连接了window.location.pathname进行重定向,否则我无法获得重定向的完整路径。如果新文件路径与旧文件路径完全不同,那么这将不起作用。

var redirectFragment = window.location.hash.replace(/#/,'/'); 
if ('' !== redirectFragment) { 
    window.location = 'http://example.com' + window.location.pathname + redirectFragment; 
} 

就我而言,我需要建立零散链接到单独的页面,这是一个什么样的通常做法,以提高网站的搜索引擎优化的一部分。