Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a situation where I want to take the following URL:

/1/john

and have it redirect using Apache's htaccess file to go to

/page.php?id=1&name=john#john

so that it goes to an html anchor with the name of john.

I've found a lot of reference to escaping special characters, and to adding the [NE] flag so that the redirect ignores the # sign, but these don't work. For example, adding [NE,R] means that the URL just appears in the browser address as the original: http://example.com/page.php?id=1&name=john#john.

like image 570
Lee L Avatar asked May 20 '10 09:05

Lee L


2 Answers

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

like image 101
credendio Avatar answered Oct 14 '22 16:10

credendio


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.

like image 26
Tony Brasunas Avatar answered Oct 14 '22 18:10

Tony Brasunas