I am trying to setup apache 2.2 with AngularJS with an almost exact structure as the one from the following closed question.
rewrite rules for apache 2 to use with angular js
I would like to redirect all requests to app/index.html except for requests to /api.
My directory structure is as followed
I am trying to apply these redirect rules in the httpd-vhosts file as such which was posted as the correct answer https://stackoverflow.com/a/15284848/671095 to the question.
<VirtualHost *:80>
ServerName www.test.com
DocumentRoot "C:/websites/test"
Alias /CFIDE "C:/websites/CFIDE"
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api
# otherwise forward it to index.html
RewriteRule ^.*$ - [NC,L]
RewriteRule ^app/. /app/index.html [NC,L]
</VirtualHost>
However when I try visit / in the browser I am presented with a blank screen. If I enter to /app/index.html in the browser however I am able to view the html page in my browser but none of the css.or js can be accessed and is returning a 404.
I have been going nuts over this which seems like a simple thing to accomplish as an apache rewrite.
Any help would be much appreciated
This is now much easier in Apache 2.2.16+ using the FallbackResource directive.
FallbackResource /app/index.html
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource
Depending on how you're forwarding access to your API you may need to also leverage the enclosure to disable the fallback resource on specifically on API requests (2.2.24+).
<Directory /api>
FallbackResource disabled
</Directory>
This works for me:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With