Reading #hash Variables in URI

Most of the time when you have a # (hash) symbol in the URI, the functionality is simply to point to an anchor on the page. This is handled by html and the functionality of your browser. In more advanced situations, this variable can be read by JavaScript to do other things like change content or tabs, or maybe even use some fancy script to scroll you to a point on the page. In the case of usability, knowing the hash variable in your PHP code can be extremely useful.

The CI IRC channel, a user by the name of amouge posted a link to his blog sharing an awesome tip on how to read this variable with your PHP code. The example he posted is addressed to CI users, but it can be used by any PHP developer using apache and mod_rewrite.

The code is as follows:


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} /([^?\ ]+)
RewriteRule (.*) index.php/%1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

view raw

.htaccess

hosted with ❤ by GitHub

You can now either parse the $_SERVER[‘REQUEST_URI’] variable now, or in CI use: $this->uri->segment();

Hope someone else finds that useful!

http://ignitedgeek.com/2010/06/01/quick-tip-hash-signs-in-uri/

Write a Comment