开发者

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

开发者 https://www.devze.com 2023-03-29 19:48 出处:网络
When a URL like /songs/123 is directly hit, I want to rewrite/redirect that URL to /index.php#/songs/123.

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.


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]


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号