apache.htaccesshttp-redirecthashtagmediatemple

.htaccess - Redirect not working as expected


I can't get this to work on my site:

# BEGIN pushState Routing
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SERVER_PORT} 80 

RewriteRule ^/tournament/(0-9)+$ /#tournament/$1 [R=301,NE,L]
# END pushState Routing

When I do ...example.com/tournament/11 I get a 404. It should redirect to example.com/#tournament/11 so that Backbone.js enables pushState and removes the hashtag itself.


Solution

  • Try this code:

    # BEGIN pushState Routing
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{SERVER_PORT} =80 
    RewriteRule ^/?(tournament/\d+)/?$ /#$1 [R=301,NE,L,NC]
    # END pushState Routing
    

    Leading slash is not matched in .htaccess so better to make it optional to make it compatible with .htaccess and Apache config.