URL rewriting - Ember Routes to URL On Apache Tomcat?

Viewed 317

I have deployed my ember app in Apache Tomcat version 9.

The index page works fine localhost:8080/myemberapp/ but when i try to access localhost:8080/myemberapp/login it returns 404 error.

I also Read about URL rewriting and added the following code snippet to .htaccess file

#html5 pushstate (history) support:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [L]
</IfModule>

And added the following to tomcat conf/context.xml file

<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />

But still I cannot access any other ember routes after deploying in Apache Tomcat.

Can anybody solve this so that, each routes in the ember app can be mapped to a custom URL.

1 Answers

I've faced this problem when I deployed my ember app in Github GH pages. What is the problem here is when you access localhost:8080/myemberapp/login route your server would look for index.html in login route or login.html file.

So In order to overcome this you'd need to mention that all routes that come after myemberapp are virtual routes, In order to mention that you'd need to change locationType property's value to hash in config/environment.js file of your Ember app Ex.

When locationType property's value is hash, # symbol will be inserted to your app's url, so your app's url would change to localhost:8080/myemberapp#/login Refernce

Example Ember app's repo and you can view that app here

Related