How to use htaccess to rewrite url to html anchor tag (#)

久未见 提交于 2019-11-28 13:18:05
credendio

This is possible using [NE] flag (noescape).

By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.

More info http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne

You can in fact do one of these things, but not both.

You can use the [NE] flag to signify to Apache not to escape the '#' character, but for the redirect to work, you have to specify an absolute URL to redirect to, not simply a relative page. Apache cannot do the scrolling of the window down to the anchor for you. But the browser will, if you redirect to an absolute URL.

What you want to do, can be accomplished with URL rewriting, or, more specifically, URL beautification.

I just quickly found this well explained blog post for you, I hope it can help you out with the learning to rewrite URLs-part.

As for the #-thing (expecting that you now know what I'm talking about), I don't see a problem in passing the same variable to the rewritten URL twice. Like: (notice the last part of the first line)

RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/#$2 [R]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&subpage=$2

Though, you'll have to escape the #-part, and it seems that it can be done this way:

RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/\%23$2 [R,NE]

BTW, URL rewriting is not that hard (but can become complicated, and I'm not an expert), but Google can help a lot along the way.

You cannot do an internal redirect to an anchor. (Just think about it: how would Apache scroll down to the anchor?) Your link should pointo to /1/john#john. Anchors aren't part of the request uri.

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