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 :)
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.
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.
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]