3

I want to run a Ajax call, but I am having issue locating the script.php file because of my htaccess rewrite rules. I have a index.php, script.php and .htaccess in the same folder. In the htaccess, there is a RewriteRule which formats a URL like this "domain.com/index.php?bla=123" into this "domain.com/123".

However, my javascript cannot locate the PHP-Script since the path is set to "script.php" when the URL becomes "domain.com/asdf/". Any idea how to run the script from the js whether through the root or a FAKE directory.

url: "script.php" || "../script.php",
6
  • Why would you need to do this? You should know where your script file is located. Commented Jan 30, 2013 at 18:34
  • So you run a call, and if it 404's run the other call? Commented Jan 30, 2013 at 18:34
  • you want a random output ?and wr is the condition ? Commented Jan 30, 2013 at 18:36
  • I know where my file is located, but i have a special rule with my htaccess. Commented Jan 30, 2013 at 18:45
  • I don't want to run everytime 2 calls for one check, isn't there any other options to check which of the paths are available. Commented Jan 30, 2013 at 18:46

2 Answers 2

1

you can use inline condition, like this example:

url: (4 > 2 ? "script.php" : "../script.php"),

in this example you will always end up with the url "script.php", so change the 4 > 2 condition to your own needs

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a possibility to check which of them is available before the call?
Look, i have a htaccess file.. if i access domain.com i need to use the url "script.php". but if i access domain.com/SOMEVALUE i need to use the url "../script.php". Any ideas how to handle that in js?
@Sylnois You could easily make an if statement that check the url of the page using window.location and based on the path use the corresponding url.
This was also my first thought, but i want to use relative paths and not absolute.. any idea how to check it?
@Sylnois I am still sort of confused. Do you only have one script file? If that is the case can't you just use "/script.php" which will take you from the root of the server directory and look for the script file.
0

Based on the further explanation of your situation, you should just be able to use "/script.php" as the path for your ajax call. In javascript, starting a path with / takes you to the root of the server; therefore, you can just progress from there and find your script files no matter where the current page is located.

If for some reason this does not work, you can use window.location.hostname to get the path to the server like so:

var scriptUrl = window.location.hostname + "/script.php";

Edit:

Based on your comments, your path would have to be your /something/script.php instead of just /script.php for both possible solutions.

8 Comments

"/script.php" didn't work. You right, my script file is in the same folder as the index.php(which a javascript is included). My htaccess has a rewriterule, that write domain.com/index.php?bla=123 into domain.com/123/ .. my problem is that the GET-Parameter doesn't have to be set. Someone can access the page without any GET-Parameter(which my htaccess-file will rewrite, and make a FAKE directory). So my problem is, that i can't handle the paths in my javascript if someone calls my site like this domain.com or like this domain.com/123/.. do you know what i mean?
My first thought was yes, but somehow it doesn't really work.
Thanks for your help. I need to update my question again, sry. I have a URL like this "domain.com/something/" or "domain.com/something/123".
So your update won't solve my problem, cause i get only the hostname.
Ah well that could be your initial problem. You would have to include the something prior to the script.php :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.