Can you write a 301 redirect to an anchor point?

一曲冷凌霜 提交于 2019-12-23 09:47:22

问题


Is this valid and correct?

RewriteRule ^myOldPage.html$ /index.php#info [R]

I'm specifically interested about the #info part.


回答1:


Yes. That's a valid 301 redirect (the HTTP standard allows for any valid URI to be provided as the redirect).

Now the caveat: Not all search engines may love the redirect. Google does a fantastic job of handling anchor tags (they even have a patent on this), while others will completely ignore them. As long as that's not an issue, the redirect is technically valid.

Update: If you're having trouble with mod_rewrite, try the NE (no escape) flag to prevent the # symbol from getting encoded:

RewriteRule ^myOldPage.html$ /index.php#info [R,NE]



回答2:


Although it looks correct, I have a strange feeling this won't work.

The browser needs to know about the #anchor. The server and mod_rewrite may well just ignore it.

If it doesn't work I guess you could do something like..

RewriteRule ^myOldPage.html$ /index.php?info=true [R]

and then in the php output a piece of javascript to do the anchor jump.



来源:https://stackoverflow.com/questions/386709/can-you-write-a-301-redirect-to-an-anchor-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!