1

I'm taking a site that used to be static html (generated by an offline tool) and converting it to PHP. I'm sending all page urls to a single PHP file which parses the url. At first I just stuck FallbackResource MyPage.php in the .htaccess file, and it worked fine... except for urls with a fragment part. Many of the old urls were like /some-page.html#part2, and in the new scheme, part2 is a whole separate page, so I can't ignore that part of the url. With FallbackResource, all that MyPage.php would ever see is /some-page.html.

Now I've read some other questions on here about the topic, such as this one, and it seemed clear that mod_rewrite should in theory be able to do the job, but I can't get it to work. Here's my most current attempt:

Options +Indexes
DirectoryIndex MyPage.php
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^:/.?#&]+\.html#(\w+)  MyPage.php#$1    [NC,NE]
RewriteRule ^[^:/.?#&]+\.(html|php)  MyPage.php       [NC]

This works, again, for everything except anchors. It works no better than the old FallbackResource command did. I've tried juggling various parts of this in random ways, such as looking for %23 instead of #, omitting the NE flag, passing the value in a querystring instead of as a fragment, and whatnot.

I will note that I don't want to use redirection to translate the urls -- the PHP has to perform a lookup on it.

Community
  • 1
  • 1
Paul Kienitz
  • 878
  • 6
  • 25
  • I'm guessing the answer is that the destination of the rewrite is a filename rather than a url. I guess I have to use [R]. – Paul Kienitz Oct 03 '15 at 23:10
  • I also tried saving the anchor in an environment var: `MyPage.php [NC,NE,E=ANCHORITE:$1]` – Paul Kienitz Oct 03 '15 at 23:17
  • I can match on any url suffix character other than a #, and pass what's after it in as a querystring, if not as a new fragment... it very much appears that the fragment is being stripped off before mod_rewrite ever sees it. – Paul Kienitz Oct 04 '15 at 01:29

1 Answers1

2

Well, I guess the answer is that it can't be done. The fragment part is never even sent to the server as part of the HTTP request. It stays inside the browser. Which means if anyone's saved an old link, it's just gonna go to the wrong page and that's all there is to it.

But I can write javascript into the page to redirect from the client. The PHP can iterate through the known list of old anchors on a given page and emit conditions to send them to the new page.

Paul Kienitz
  • 878
  • 6
  • 25