0

I have a basic web page with a few divs on it with the ids one to ten. When I visit a URL like this:

www.example.com/page.php#six 

the page will load and I will be looking at div id six.

I'd like a RewirteRule which allows a url like this:

www.example.com/page/six 

and the page will load and I will be looking at div id six.

The rule I'm trying to use at the moment goes as follows but does not work:

RewriteRule ^page/six /page.php\#six [L,QSA]

Help is greatly appreciated :)

Ed Hardie
  • 13
  • 2
  • 5
    anchor parts of an url (i.e. the part after the #) is handled client side only. You can't do that. – Fabio Aug 23 '11 at 10:18

3 Answers3

4

URL fragments (the #bit) are NOT sent to the server (or at least, they shouldn't be). Your browser sends a normal GET request, minus the #bit. When the page response is received and the page rendered, your browser automatically scrolls to that id. So your scheme will not work.

Also, I'm pretty sure you're not using ReWrite correctly. As far as I understand this is used to map requested assets to some physical disk location, it does not issue a redirect.

Edit: ReWrite can be used to issue a redirect with the [R] flag, per @Hakre's comment below. So in that case you want to redirect from /page/six to /page#six, which providing Apache doesn't choke on the #six should I guess work.

Richard H
  • 38,037
  • 37
  • 111
  • 138
  • +1 for the correct explanation. FYI: `[R]` does issue a redirect, it's a flag you can use with ModRewrite. – hakre Aug 23 '11 at 10:26
  • @hakre, yes of course. So maybe his idea could work with [R] ? – Richard H Aug 23 '11 at 10:30
  • 1
    It is possible to redirect to a url containing a url fragment, but not possible to use one in `redirectcond` statements. However, internal subrequests containing fragments are somewhat silly and most certainly useless. – nikc.org Aug 23 '11 at 10:30
2

Your rewrite rule is missing the R flag (redirect), as pointed out by @hakre in the comments in @Richard's answer. But you also need to include the NE flag (no escape), otherwise the pound sign will be translated into %23, which doesn't work as expected.

RewriteRule ^page/six /page.php#six [R,NE,QSA,L]
nikc.org
  • 16,462
  • 6
  • 50
  • 83
0

There is another article on StackOverflow that is similar to your scenario that may be of help: .htaccess redirect with fragment

I'm not in front of a server right now so can't test this out but maybe the linked article will shed some light on things in either direction.

Community
  • 1
  • 1
Liam
  • 1,712
  • 1
  • 17
  • 30