19

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lee L
  • 191
  • 1
  • 1
  • 3

4 Answers4

28

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

Lefteris
  • 3,196
  • 5
  • 31
  • 52
credendio
  • 281
  • 3
  • 4
6

You can 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.

Tony Brasunas
  • 4,012
  • 3
  • 39
  • 51
  • 1
    To the original poster: indicate an answer as correct to resolve this. – Tony Brasunas Jun 06 '13 at 17:21
  • Hmm .. with Apache 2.4.25, I'm specify a relative page and still getting an external redirect to an anchor tag. Maybe things have changed since 2013? In a section, `RewriteBase "/"` then `RewriteRule ^services/?$ #services [L,R=301,NE]` is working for me. Which is nice if I don't have to re-do the rules when converting to https. – Mark Berry May 09 '17 at 01:30
3

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.

Sune Rasmussen
  • 956
  • 4
  • 14
  • Hi Sune, thanks for the response. I don't think what I'm trying to do is possible, however. See response on this page from nikc. – Lee L Jun 06 '10 at 13:53
0

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.

nikc.org
  • 16,462
  • 6
  • 50
  • 83
  • I'm verifying this in practice and believe you're correct. The browser interprets anchors and scrolls down to the correct point in the page; an Apache redirect is not able to do this because the redirected-to URI isn't passed to the browser. – Lee L Jun 06 '10 at 13:50