Saturday, August 8, 2015

AngularJS Project Template and Apache 2.2/2.4 setup

On my Github, I've created an AngularJS project template that sets up Grunt, LibSass, and HTML5Mode.

https://github.com/pbrizardo/grunt_libsass_angular_scaffold

Setting up Apache 2.2/2.4 to support Html5Mode

Use Apache's mod_rewrite to forward the index.html for any URL requests through the use of rewrite rules.

1) Put all rewrite rules in httpd.conf.
2) Edit httpd.conf and put rewrite rules in .htaccess

Method 1 will be more convenient since all changes are done in one file.
Method 2 might be convenient for those who want different rewrite behavior in different applications (Never tried this)

Method 1

1. Open httpd.conf
2. Find the Directory tag for the served documents.

Ex. <Directory "/apache2.2/htdocs"> or <Directory "var/www">

3. Add the following rules:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteCond %{REQUEST_URI} !.*\.(css|js|html|png)
    RewriteRule (.*) index.html [L]

4. Make sure AllowOverride None is set

That's it!

Method 2

1. Open httpd.conf
2. Inside the <Directory /> tag, make sure it has the follow options:

<Directory />
    Options All
    AllowOverride All
</Directory>


3. Change all instances of AllowOverride None to AllowOverride All
4. Open .htaccess file
5. Insert the following at the end of the file

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteCond %{REQUEST_URI} !.*\.(css|js|html|png)
    RewriteRule (.*) index.html [L]
</ifModule>

6. If running Apache 2.4, go to the next step, otherwise, you're done!
7. Due to change in new directives access control modules, directives such as Order, Deny, and Satisfy have been replaced

http://httpd.apache.org/docs/2.4/upgrading.html

So you'll find the old directives under the tag  <FilesMatch "(^#.*#|\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|sw[op])|~)$">

Replace all directives with

Require all denied

No comments:

Post a Comment