Apache rewrite rule for HTML5 browser history URL bookmarkability

I found a bunch of examples on the web of Apache rewrite rules that people are using for AJAX-based HTML5-history-using sites. All of them didn’t work for me. I don’t know why. But I thought it was Ember’s fault. It wasn’t. So I spun my wheels for a while blaming the router. It wasn’t. It’s just that there are some strange examples of rewrite rules on the web for AJAX-based HTML5-history-using sites. Anyway, I’m posting so nobody else gets caught up blaming Ember’s router. We can blame the router for other things :wink: JK: I’m in love with Ember, so I can tease. Maybe this will help someone else who’s just getting started.

Below is what worked for me. I wanted to send every request for URLs to index.html, except for certain subdirectories (js, css, img, svc). If you do this in the root of your Apache config, you might need to specify a forward slash. If you don’t use Apache, then you don’t care :smiley:.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(js|css|img|svc)($|/) - [L]
RewriteRule ^(.*)$ index.html [L]
3 Likes

Thanks for the post. I’ve been looking to do the same thing too. I found this post helpful: http://readystate4.com/2012/05/17/nginx-and-apache-rewrite-to-support-html5-pushstate/

3 Likes

This helped a lot in configuring routes for my app. Thanks for the link !!

Thanks, all! This was my savior after hours of searching and tweaking. The solution on the page provided by @owz ended up being what I needed.

Also, in Apache 2.2.16+, you can use a much simpler method which I have confirmed works:

FallbackResource /index.html
7 Likes

As of this date, this worked great for me with ember 1.11.1 etc. CLI 2.2

The Apache rewrite rules mentioned in @owz’s post worked for me for Apache 2.2.31:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
 </IfModule>

@joshkg I’m using Apache 2.4 and facing this same issue. Where exactly did you include this configuration? In the httpd.conf file? If so, in what part of the file? Inside a <VirtualHost> tag or outside? Could you share a code snippet of your config file including this config “surrounds”, please? Thank you

Cool. Is this supposed to work for all routes, or only root index? Doesn’t seem to work for me on individual routes.

I actually have mine in a .htaccess file.

I ended up using the same solution as @abobwhite mentioned. Very simple and efficient. I just added

FallbackResource /index.html

at the end of

/etc/httpd/conf/httpd.conf

file.

1 Like

I tired so many setting final this solution work for me. In my case I applied both as following:

  1. update .htaccess file
  2. As a backup append line in “FallbackResource /” /etc/apache2/apache2.conf

Note:

  • In machine I don’t have httpd.conf file after install apache2. I used to update apache2.conf file.
  • I removed index.html because this can be vary if we use .php, .html or any other file that’s why I use root option as “/” only.

But my problem solved after 2nd step.