如何使用 mod_rewrite/htaccess 通过锚点检测 URL 并重定向?

发布于 2024-09-13 02:02:12 字数 510 浏览 5 评论 0原文

我见过很多相反的例子,但我希望从锚点/哈希 URL 转到非锚点 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) 都没有看到它。

有什么想法、过去的经验或成功吗?

I've seen a number of examples of the opposite, but I'm looking to go from an anchor/hash URL to a non-anchor URL, like so:

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

None of the examples at http://karoshiethos.com/2008/07/25/handling-urlencoded-swfaddress-links-with-mod_rewrite/ have functioned for me. It sounds like REQUEST_URI has the /#/stuff in it, but neither me nor my Apache (2.0.54) see it.

Any ideas, past experiences or successes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金橙橙 2024-09-20 02:02:12

# 之后的任何内容都是片段,并且不会被发送到网络服务器。您无法在任何时候捕获它,您必须使用客户端方法来捕获它们。

Anything after the # is a fragment, and will not be sent to the webserver. You cannot capture it at any point there, you'll have to use a client-sided approach to capture those.

人生戏 2024-09-20 02:02:12

@RobRuchte:使用 window.hash 不是更好吗? location.hash,用替换而不是正则表达式?

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

@RobRuchte : would it not be better to use window.location.hash, with a replace instead of a regular expression?

var redirectFragment = window.location.hash.replace(/^#/,'');
if ( '' !== redirectFragment ) {
    window.location = 'http://new.html-site.com' + redirectFragment;
}
忘羡 2024-09-20 02:02:12

我是您链接到的帖子的作者。 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];
}

I'm the author of the post you linked to. Wrikken is correct, the content after the named anchor is not sent to the server unless something has mangled the URL along the way. On the client side, you need some JavaScript like this in your landing page to redirect the swfaddress links to corresponding URLs on another domain:

var re = new RegExp('#(.*)');
var redirectFragment = re.exec(document.location.toString());
if (redirectFragment!=null)
{
    document.location = 'http://new.html-site.com'+redirectFragment[1];
}
别念他 2024-09-20 02:02:12

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

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

就我而言,我需要在各个页面中构建碎片链接,这是改进网站搜索引擎优化通常所做的一部分。

I used a modified version of the answer by @m14t. This works for redirects that look like http://example.com/path/to/page#fragment --> http://example.com/path/to/page/fragment. Notice that I also concatenated the window.location.pathname for the redirect, otherwise I would not get the full path for the redirect. If the new file path is completely different from the old one, then this would not work.

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

In my case, I needed to build fragmented links into individual pages, which is part of what is commonly done to improve a website's SEO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文