dealing # (hash symbol) in URLs

Oh, the strange things mod_rewrite does!

dealing # (hash symbol) in URLs

Postby jsitke » Thu Jul 26, 2007 12:15 pm

This is a bit frustrating. It seems like mod_rewrite is misinterpreting the #.

I have tried:
Code: Select all
RewriteRule ^x.html#bookmark/page$ /page.html [L]


and also escaping the hash (#):
Code: Select all
RewriteRule ^x.html\#bookmark/page$ /page.html [L]


Neither seems to work. I believe .htaccess interprets # as a comment.
jsitke
 
Posts: 3
Joined: Sat Mar 12, 2005 10:00 am

Postby richardk » Thu Jul 26, 2007 2:33 pm

The fragment (the bit after the #) is never sent to the server so you cannot match it with mod_rewrite.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jsitke » Thu Jul 26, 2007 2:42 pm

Is there another way such as passing the hash as an ascii value to mod_rewrite? I really have to match on that.
jsitke
 
Posts: 3
Joined: Sat Mar 12, 2005 10:00 am

Postby richardk » Thu Jul 26, 2007 2:57 pm

The only thing you can do is use %23 instead of #, eg /page.html%23abc, but then it won't work as a fragment in the browser. So no, not really.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby htaccesselite » Fri Oct 05, 2007 3:11 am

I also ran into this problem because some http clients were making requests for links like site.com%23comment5 instead of #comment5, so I was able to use code from Replacing ‘%23′ with ‘#’ in incoming links which is:

Code: Select all
RewriteRule ^(.*).html([^c]+)comment(.+)$ http://www.site.com/$1.html#comment$3 [R=301,L,NE]


So to fix your issue jsitke I'd do the same thing as the above code, basically instead of trying to specify the # you specify everything EXCEPT what comes after #, in your case bookmark.. something like..

Code: Select all
RewriteRule ^x\.html([^b]+)bookmark/page$ /page.html [L,NE]
htaccesselite
 
Posts: 6
Joined: Tue Nov 07, 2006 4:20 am
Location: http://groups.google.com/ group/apachehtaccess

Postby cindobley » Thu Apr 03, 2008 10:07 am

Is it possible to go the other way with this? I need to write a hash into a URL from my RewriteRule. Currently trying to use
Code: Select all
RewriteRule ^((?:[a-z0-9-_]+/){0,7})([a-z0-9-_]+)$ /index.html#/$2/ [R]
but it is inputing a %23 in instead of a # character.

Thanks.
cindobley
 
Posts: 1
Joined: Thu Apr 03, 2008 10:00 am

Postby richardk » Thu Apr 03, 2008 3:42 pm

Try NE (No Escape) flag
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^((?:[a-z0-9-_]+/){0,7})([a-z0-9-_]+)$ /index.html#/$2/ [NE,R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 4 guests

cron