I have a script containing PHP and JavaScript files.
All these files are in the root folder /myfolder.
Let’s say the script I have to include on my website is /myfolder/script.js , the problem is that in script.js I have ajax calls to ../myfolder/ajax.php, which, because the path will be relative to the page the script is included won’t work if I had something like this on the page /a/b/page.php:
<script src="../../myfolder/script.js><script> because this will call the ajax method from ../myfolder/ajax.php as stated in the AJAX call, which in this case is /a/myfolder/ajax.php.
How can I rewrite the AJAX call URL so that it will always point to the right file regardless where the script.js is included?
ROOT
|---myfolder
|--script.js
|--ajax.php
|--page1.php
|--subfolder
|--page2.php
|--subfolder
|--page3.php
on a URL like
http://www.example.com/ajax:will return
/ajax/myfolder/ajax.phpsimilarly on a URL like
http://www.example.com/:will return
/myfolder/ajax.phplastly on a URL like
http://www.example.com/myfolder/a/b/c/d/e/f/g/:will return
/myfolder/ajax.php