views:

179

answers:

4

For example, I'd like to have my registration, about and contact pages resolve to different content, but via hash tags:

three links one each to the registration, contact and about page -

  • www.site.com/index.php#about
  • www.site.com/index.php#registration
  • www.site.com/index.php#contact

Is there a way using Javascript or PHP to resolve these pages to the separated content?

+5  A: 

The hash is not sent to the server, so you can only do it in Javascript.

Check the value of location.hash.

SLaks
A: 

You can use PHP's Global $_REQUEST variables to grab the requested URL and parse out the hashtag...

Anthony M. Powers
Wrong. Hashtags are not normally sent to the server but are handled by the user agent. You're probably thinking of the cgi params, not the hash (or fragment identifier).
Alan
Hashtags are sent to the server in the request. Check it out yourself before calling someone wrong in the public community.
Anthony M. Powers
A: 

I suppose you probably have a good reason, but can I ask, why would you do this? It breaks the widely understood standard of how hashs in URLs are supposed to work, and its just begging for trouble for interoperability with other clients, down the road.

Spike Williams
This is commonly done in AJAX-y sites like Gmail to allow the user to retain browser history and bookmarking capability despite the fact that the actual page URL is never changing.
ceejayoz
Interesting technique, I hadn't heard of that before.
Spike Williams
Well for SEO it's beneficial sometimes to reduce the linking "footprint" that certain pages impose from a site architecture standpoint. With these sorts of pages, they don't really help in search results - they are rather support pages for users and if consolidated can help to put emphasis on other content/URLs...Makes sense?
Rninja
Sounds like SEO voodoo to me :) Better, I think, to have those resolve to real pages, have them link back to the main page, and have Google recognize you as a complete site with ancillary pages, rather than risk having them penalize you for breaking web standards.
Spike Williams
There would only be penalty if you were misleading users. In this case there is no misleading - users are taken to exactly what they expect, but technically enables a potential advantage.
Rninja
+2  A: 

There's no server-side way to do it. You could work with AJAX, but this will break the site for non-javascript users. The best way would probably be to have server-side content URLs (index.php?page=<page_id>) and rewrite these locally with JavaScript (to #<page_id>) and handle the content loading with AJAX then. That way you can have your hash-URLs for JS-enabled devices and everybody else can still use the site.

It does however require a bit of redundance because you need to provide the same content twice, once for inclusion via AJAX and once with the proper layout and everything via PHP.

If you just want hash URLs for aesthetic reasons, but don't want to rely on JS, you're out of luck. The semantics of URLs are against you: fragment IDs shouldn't really affect the content the URL is referring to, merely the fragment within that content. AJAX URLs are changing those semantics, but there's no good reason to do that if you don't have to.

Alan
This looks good (and thanks to Slaks too). I'll give it a whirl and see what I get. Probably going to have to test quite a bit - very rusty in any javascripting.
Rninja