1

I've run into an issue, which doesn't seem new or exciting, but of all the answers I've found none seem to work. I feel like programming for .htaccess there are a million ways to solve a problem but I can never find/use the correct one.

I would like to take an already clean URL:
http://example.com/page/link

and do an internal rewrite to:
http://example.com/page#link

My sources before asking:

  • http://stackoverflow.com/questions/2872490/how-to-use-htaccess-to-rewrite-url-to-html-anchor-tag
  • http://stackoverflow.com/questions/21391449/how-to-use-sign-as-an-anchor-while-url-rewriting-with-htaccess-on-apache
  • http://stackoverflow.com/questions/29165917/htaccess-rewrite-url-to-hash-anchor-on-homepage
  • http://stackoverflow.com/questions/2686075/mod-rewrite-with-anchor-link
  • http://stackoverflow.com/questions/2285193/htaccess-redirect-to-page-with-anchor-link

I've read over all the answers and comments to find a solution to my particular issue. Nothing has worked so far. I can't even get a simple test to work and it seems as if the rewrite isn't working at all.

You can find my current .htaccess file here: http://pastebin.com/xJWHq70f

NOTE: Web host is Netfirms. I've disabled Netfirms' default .htaccess foolery.

So to recap, I'm trying to take an already clean URL and further parse and rewrite from page/test to page#test. The last line in my .htaccess did have

RewriteRule ^about-me/(.*)$ /about-me#$1 [NE,R] (didn't work)

In theory creating a link on a page to about-me/who-am-i should take me to http://example.com/about-me#who-am-i while keeping the address bar looking like http://example.com/about-me/who-am-i. At this time I have one specific page I'd like this rewriting to be enabled for, the about-me.php page.

Thanks,
Doug

PS
Sorry for not being 10+ rep so I can post all the links normally

UPDATE Jan 27 2016

Further review and further investigation proves what I'd like done simply cannot be done. No problem. I've switched gears and changed the way I'd like my navigation system to work.

Instead I will use the about-me page as a base and create the extra necessary pages needed. As there is a lot of text on the page it's better to break it up into nicer to read sections - now separate pages.

Using the idea William presented I was able to quickly forge together a solution. My original .htaccess code to remove the .php extension and then to redirect a .php extension to the non-extension equivalent was broken and caused the 500 errors. FIXED!

Using the following code:

RewriteRule ^about-me/(.*) /$1 [L,NC]

I can have any URL about-me/stats or about-me/testing123 and as long as the actual stats.php or testing123.php exists in the root it will be internally redirected properly - keeping a nice clean URL while 404'ing if the file doesn't exist.

  • 1
    as far as I know anchor tags are dealt with on the client side to allow jumping to a page section. If you do an internal rewrite the client won't know that. – apokryfos Jan 27 '17 at 07:21

1 Answers1

0

Ok the important thing is to make sure you do an internal rewrite, not an external rewrite

So this will work:

RewriteRule ^page/(.*) /page#$1 [L]

Make sure you do not add the [R=301] flag (or any other value for R) or it will provide a response to the client. As per RFC 3986 and RFC 1738, the fragment identifier is removed by clients when dereferencing a URI.

EDIT

Although Apache will rewrite the URL to include the fragment, PHP will probably barf. As per RFC 1738:

The character "#" is unsafe and should always be encoded because it is used in World Wide Web and in other systems to delimit a URL from a fragment/anchor identifier that might follow it...All unsafe characters must always be encoded within a URL... For example, the character "#" must be encoded within URLs even in systems that do not normally deal with fragment or anchor identifiers, so that if the URL is copied into another system that does use them, it will not be necessary to change the URL encoding.

So you will need to escape the fragment if you want PHP to safely parse it.

William Greenly
  • 3,914
  • 20
  • 18
  • Hey William, I just tried that snippet and still getting 500 errors. I've even tried deleting all other .htaccess coding and just kept that single rewrite line. – Doug.Fredrick Jan 27 '17 at 07:42
  • Are you sure the 500 isn't coming back from the PHP server. The fragment identifier might be causing it to throw an error since technically it needs to be escaped to qualify it as a URL. How do you hand the URL in PHP? – William Greenly Jan 27 '17 at 07:47
  • Quick update: I got your code to work, sort of, as in no more 500 error. I added back my full .htaccess code and commented out the section I have for the clean urls. So now if I use:
    RewriteRule ^about-me.php/(.*) /about-me.php#$1 [L] it takes me to the about-me.php page (nice), shows the url as example.com/about-me.php/test (very nice) but now just sits at the top of the page instead of letting the browser jump down to the anchor. Something is very wrong with my clean URL code...
    – Doug.Fredrick Jan 27 '17 at 07:57
  • Not sure what you mean by escape and parse in PHP. I have 0 php code on my pages. The only reason I used PHP was in case I need to code something special - which sounds a lot like this very scenario. – Doug.Fredrick Jan 27 '17 at 08:04
  • Rewriting to # is imposible as # doesnt get to the server. However using R flag, its possible to redirect an uri to fregment. – Amit Verma Jan 27 '17 at 08:15
  • @starkeen I've been reading that too. I also found this resource [link]http://httpd.apache.org/docs/2.2/rewrite/advanced.html#redirectanchors which leads me to believe it's possible without using R flag – Doug.Fredrick Jan 27 '17 at 08:18
  • @Doug are you kidding? – Amit Verma Jan 27 '17 at 08:25
  • The R flag creates a HTTP 30x response which gets passed back to the HTTP client. Subsequent requests will therefore cut of the fragment. Without the R flag, it will rewrite the URL to include the fragment, which is technically 'unsafe'. – William Greenly Jan 27 '17 at 09:32