[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

Anchor instead of comment in htaccess

Is there a way in an htaccess file to interpret the # literally, and not as a comment?

For example I'm using mod_rewrite and I'd like to take all incoming links to a particular folder, and redirect them to a certain page with an anchor at the end.

Something like this:

RewriteRule ^blogposts/([^/\.]+)/?$ blogposts/index.php?title=$1#$1
0
bswinnerton
Asked:
bswinnerton
  • 3
  • 2
3 Solutions
 
miahiCommented:
Try replacing the "#" sign with "\%23" in that link: blogposts/index.php?title=$1\%23$1
0
 
miahiCommented:
You also need the [NE] flag set:  blogposts/index.php?title=$1\%23$1 [NE]

'noescape|NE' (no URI escaping of output)
    This flag prevents mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from happening. This allows percent symbols to appear in the output, as in

    RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]
    which would turn '/foo/zed' into a safe request for '/bar?arg=P1=zed'.
0
 
bswinnertonAuthor Commented:
Hmm, I'm having a little bit of trouble because my browser is thinking that the %23 is part of the variable that I'm passing.

For example, if I echo $_GET['title'] I get the entire title and then #title right after it. Is there a way to signify the end of passing a variable? Or is there another solution?
0
 
bswinnertonAuthor Commented:
P.S. this only happens if I type %23 in the browser, if I use the actual pound sign, it works fine
0
 
miahiCommented:
I'm no master of htaccess, but I think that NE flag is what is missing in your first attempt (the one with the #).

http://httpd.apache.org/docs/2.2/rewrite/rewrite_flags.html (the official doc says you can have that # there)

NE|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.

RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]

The above example will redirect /anchor/xyz to /bigpage.html#xyz. Omitting the [NE] will result in the # being converted to its hexcode equivalent, %23, which will then result in a 404 Not Found error condition.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now