[removed] with hash part (anchor) #

2019-02-03 02:07发布

One of our website has URL like this : example.oursite.com. We decided to move our site with an URL like this www.oursite.com/example. To do this, we wrote a rewrite rule in our Apache server that redirect to our new URL with a code 301.

Many websites link to us with URLs of the form example.oursite.com/#id=23. The problem is that the redirection erase the hash part of the URL with IE. As far as I know, the hash part is never sent to the server.

I wanted to implement the redirection with javascript to keep the hash part, but the Search Engine will not be aware that our URL changed. (no code 301 returned)

I want the Search Engine to be notified of our new URL(301) because we need to transfer the page rank to our new URL.

Is there a way to redirect with a 301 code and keep the hash part(#id=23) of in the URL ?

8条回答
冷血范
2楼-- · 2019-02-03 02:31

I registered my account so I can't edit.

zombat : I'm sorry I made a mistake in my comment. The link to our video is exemple.oursite.com/#video_id=233. In this case, my rewrite rule in Apache doesn't work.

Nick Berardi: We changed the way our links work. We don't use # anymore, only for backward compatibility

查看更多
The star\"
3楼-- · 2019-02-03 02:33

Search engines do in fact care about hash tags, they frequently use them to highlight specific content on a page.

To the question, however, anchor locations are unfortunately not sent to the server as part of the HTTP request. If you want to redirect a user, you will need to do this in Javascript on the client side.

Good article: http://web.archive.org/web/20090508005814/http://www.mikeduncan.com/named-anchors-are-not-sent/

查看更多
冷血范
4楼-- · 2019-02-03 02:33

You could create a page on the old address that catches all requests and redirects to the new site with the correct address and code.

I did something like that, but it was in asp.net, which I guess it's not the language you use. Anyway there should be a way to do this in any language.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-02-03 02:39

Seeing as the server will never see the # (ruling out 301 Redirects) and Google has deprecated their AJAX Crawling scheme, it seems that a front-end solution is the only way!

How I did it:

 (function() {

    var redirects = [
        ['#!/about',         '/about'],
        ['#!/contact',       '/contact'],
        ['#!/page-x',        '/pageX']
    ]

    for (var i=0; i<redirects.length; i++) {
        if (window.location.hash == redirects[i][0]) {
           window.location.replace(redirects[i][1]);
        }
    }

 })();

I'm assuming that because Google crawlers do indeed execute Javascript, the new pages will be indexed properly.

I've put it in a <script> tag directly underneath the <title> tag, so that it get executed before any other JS/CSS. Note that this script should only be required for your index file.

查看更多
对你真心纯属浪费
6楼-- · 2019-02-03 02:40

I am fairly certain that the hash/page anchor/bookmark part of a URL is not indexed by search engines, and therefore has no effect on your page ranking. Doing a google search for "inurl:#" returns zero documents, so that backs up my assumption. Links from external sites will be indexed without the hash.

You are right in that the hash part isn't sent to the server, so as far as I am aware, there isn't a good way to be able to create a redirection url with the hash in it.

Because of this, it's up to the browser to correctly manage the hash during a redirect. Firefox 3.5 appears to do this successfully. If you append a hash to a URL that has a known redirect, you will see the URL change in the address bar to the new location, but the hash stays on there successfully.

Edit: In response to the comment below, if there isn't a hash sign in the external URL for the part you need, then it is entirely possible to rewrite the URL. An Apache rewrite rule would take care of it:

RewriteCond %{HTTP_HOST}   !^exemple\.oursite\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.oursite.com/exemple/$1 [L,R]

If you're not using Apache, then you'll have to look into the server docs for something similar.

查看更多
我命由我不由天
7楼-- · 2019-02-03 02:55

Google has a special syntax for AJAX applications that is based on hash URLs: http://code.google.com/web/ajaxcrawling/docs/getting-started.html

查看更多
登录 后发表回答