3

I believe this is an Apache .htaccess issue, and yet, maybe not. Maybe I am looking at the problem from the wrong angle, and thus can't find the proper solution.

I am building a web app + hybrid mobile app. I would like to share the exact same code base, without having to tweak anything manually to deploy my app to Android or iOS, otherwise, the process of deploying will be hacky and painful. What I want is to take the web app repository, shove it into Cordova's box (you dirty man ;), and it would deploy it successfully.

Now, one issue is that Cordova requires relative paths to work properly. For example, this is how I include my require.js file :

<script data-main="library/js/dependencies.js" src="library/js/libs/require.js">
</script>

This works fine on the hybrid app. This works fine also on most of the web app's URLs, those with the following scheme :

domain.com/view_name

However, this is what happens when I load the app from a view that receives URI parameters :

domain.com/view_name/6iwO4NyJqy

The relative paths are not resolved properly anymore. I get 404 error due to unproper paths. For instance, this is how is resolved the require.js file above :

http://domain.com/view_name/library/js/libs/require.js

The view_name bit is the wrong part. It should not be there. Without it, the file would be found successfully.

This is my .htaccess file :

RewriteEngine on
RewriteBase /

# REROUTING EVERYTHING TO index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /index.html [NC,L,QSA]

Is there a way to set my .htaccess file, so that I don't need to modify the relative paths within the app, and still can have them resolved properly ?

Any suggestion is most welcome.

Alexandre Bourlier
  • 3,972
  • 4
  • 44
  • 76
  • 1
    It is not caused by your rewrite rule, it is due to your use of relative paths. You can add this just below `` section of your page's HTML: `` – anubhava Jan 25 '16 at 14:26
  • 1
    You rock dude. Thanks. I didn't know about this `` tag, thus could not formulate the solution properly. You should make this comment an answer ;) – Alexandre Bourlier Jan 25 '16 at 14:43

1 Answers1

2

It is not caused by your rewrite rule, it is due to your use of relative paths.

You can add this just below <head> section of your page's HTML:

<base href="/" />

so that every relative URL is resolved from that base URL and not from the current page's URL.

anubhava
  • 761,203
  • 64
  • 569
  • 643