matt
matt

Reputation: 123

How can I use PHP/Apache (mod_rewrite) to redirect URLs as hash tags?

When a URL like /songs/123 is directly hit, I want to rewrite/redirect that URL to /index.php#/songs/123.

I figure using the server to do this is more efficient than creating "dummy" pages that redirect.

For example, I use jQuery.address to 'catch' any <a/> tags and append its href as a hash tag, allowing the site to be navigated seamlessly and have browser history maintain accuracy. I still want these hrefs to be valid links to the appropriate content however in case someone opens it in a new tab or similar.

Upvotes: 3

Views: 1775

Answers (2)

Michael Petrov
Michael Petrov

Reputation: 2307

Try something like this (in your .htaccess):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/songs/(.+)$ /index.php#/songs/$1 [R,L,NE]
</IfModule>

Or a more extreme case that captures anything that isn't a file/directory:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php#$1 [R,L,NE]
</IfModule>

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

Since the URL fragment only matters on the client, you need to force the client to redirect.

RewriteRule ^/songs/(.*) /index.php#/songs/$1 [R,L,NE]

Upvotes: 7

Related Questions