Change the root path for all ajax (mod rewrite) - php

My all the ajax calls lie inside the js files and calls are done using jquery. I was using relative url in these files for the ajax call.
I just implemented pretty urls and all the ajax calls have stopped working + All the css,images and links stopped working.
I fixed the images,css,js includes by using
<link href="<?php echo ROOT; ?>/myfile.css" rel=stylesheet />
where ROOT -> root path for my website
So, i switched over to absolute urls.
But still i cannot figure out a way to modify urls in ajax calls as i cannot use <?php echo ROOT; ?> inside a .js file.
$.post('relativeUrl.php',params,function(){
});
Anybody have a solution for this. Thanks in advance.
Please ask for clarification if I didn't explained things well

Related

Root directory path problems

I am working on a website for a client and I am using xampp to serve and work on locally.
The site is loading fine at localhost/site but the resources are not being found. The previous developer used absolute paths in the index.php to define resources like so:
<script href="/includes/javascript.js">
The site fails to load javascript.js because it looks at:
localhost/includes/javascript.js
When it NEEDS to be:
localhost/site/includes/javascript.js
index.php is located here:
localhost/site/index.php
It works fine on the client server. What am I missing here?
A URL starting with / is always absolute, you need a relative path here. So use
<script href="includes/javascript.js">
in your index.php or all files located in localhost/site or use
<script href="/site/includes/javascript.js">
for use in all files.
You have a two mistake use src not href and remove / from start. try like that
<script type="text/javascript" src="includes/javascript.js"></script>

Moving website to localhost: Document Root changed

I have download a website that was written from scratch using PHP, MySql, html, css, jQuery.
After fixing some configuration with MySql and htaccess, I get to a point where all my CSS and JS files are not loaded because of a relative path error.
This is how it looks:
The error in the console GET http://localhost/fonts/fonts.css sends me to index.php which loads all those files with a relative path.
Now, if I remove the / at the beginning of each line, the files will be loaded, however, I get more errors on different pages with bad URLs and such, that means that I need to fix this / problem instead of just renaming the hrefs.
So my question is, how to fix it?
I have tried to set a base href tag:
<base href="http://localhost/israelrescue/">
or
<base href="http://localhost/israelrescue">
or in PHP:
$_SERVER['DOCUMENT_ROOT'] = 'http://localhost/israelrescue';
or
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/israelrescue/';
P.S:
israelrescue is the name of the folder I am using in my localhost installation for this website.
Nothing seems to work, any ideas?
Thanks!
Answers:
It's because you've got the site in a subfolder in localhost. Is it possible for you to move the site into the root? I think #MukeshPanchal is telling you to load the assets from live, which is missing the point somewhat. – thebluefox
Looks like this site was not written to be run from within a
sub-folder. Easiest way to work around that would be to set up a
VirtualHost for this project in your local server, so that you can
load it directly via a local domain name, without any additional path.
(And btw., setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.)
– CBroe
You can leave the site in its subfolder as long as you drop the "/" in the beggining of every file path :
<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/common.js"></script>
becomes
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/common.js"></script>
Should work like this

Change working directory in PHP doesn't affect HTML src

I have a php script in a folder "root/test/index.php" which calls another php script located in "root/app/run.php"
In the run.php file I change the php's working directory with a call to chdir to simplify my require_once paths
All the php includes work fine, however HTML paths are totally messed up.
To make my style.css works I have to use a path relative to "root/test/" even if my style.css is in "root/app/".
This works:
<link rel="stylesheet" type="text/css" href="../app/style.css">
and that goes on for all HTML sources like img etc.
How can I change HTML working directory too?
I looked in php/HTML documentation, but I have find no good solution.
Any clue?
Thanks in advance.
HTML don't know anything about the working directory of your PHP script.
The HTML is sent to the client who can't know what was the working directory.
You could add some redirection in an .htaccess file (if you are using apache) to redirect root/test/ to root/app or something like that.
Here is how it works :
You browser ask for /root/test/index.php
Your server handle all his things and then output HTML file
Your browser parse the HTML file
If it find something like that it will search the image here : /root/test/images/cat.png because you are viewing the /root/test/index.php file and you provide a relative path
Add a comment if you have more questions.

Hosted Site Not using CSS/JQuery

I have created a site, running it locally using XAMPP as i developed it, and it runs perfectly when local.
However i tested it when hosted and the CSS & JQuery (Google library) do not work at all.
The CSS tags use the standard:
<link rel="stylesheet" type="text/css" href="/css/css.css">
Is there a reason this wouldnt work when live? But surely this has nothing to do with why JQuery doesnt work either?
I dont feel comfortable leaving it live when it doesnt work, so im sorry i cannot give you a live version to view.
Any help would be great!
Do check with Inspector if the CSS and JS files are loaded correctly when you go live — do note that the first slash in front of the URL means you're referencing the root URL of the live site. Are the directory structures of the local and live site different?
Also, you can try loading the external files using the absolute URL, like http://domain.tld/css/css.css (double check if the file exists, or loadable). If it works, then it's your relative URL path that is not working.
[Edit]: For the sake of completeness, this is how relative URL paths work:
/<fileName>.<fileExtension> will point to the file at the root directory
./<fileName>.<fileExtension> will point to the file in the same directory as the current page
../<fileName>.<fileExtension> will point to the file in the directory one level above, relative to the current directory
Check that you have actually uploded CSS files into /css/ folder. (Obviously /css/ folder should be present on your server)
Make sure you can access you CSS files from the web: eg if you go herehttp://yourdomain.com/css/css.ss you should see the contents of you CSS file.
if you can see the contents of your file here: http://yourdomain.com/css/css.css
then try this:
<link rel="stylesheet" type="text/css" href="http://yourdomain.com/css/css.css">
And as #Terry already mentioned check the paths.

linking to css in localhost getting lost in multiple pages

I just recently installed XAMPP and have been trying to get my former website which was hosted online to work in it.
Everythings okay until I navigate past my baseurl. To make matters a bit more complicated, i've been the page using jquery-mobile so all the content is fetched through AJAX.
baseurl = 127.0.0.1/mobilebrowsergame/
When I refresh(start) on this page, everythings okay because it uses the default root for the CSS files.
However, if I start/refresh on a page a bit deeper, ie.
127.0.0.1/mobilebrowsergame/structure/1
The css obviously doesnt work.
I originally included <?php echo base_url() ?>css/stylesheet.css ?> but was forced to remove the base_url() because it wasn't loading under localhost.
At this point, I'm at a loss. I don't know how to tackle this without creating a new CSS for ever page under the base.
Any ideas?
It looks like you are pointing to your CSS files using a relative URL. If you prepend a path with /, it will automatically start at the web serve root (in this case, 127.0.0.1), instead of at the current directory.
i.e. Try this
<link rel="stylesheet" type="text/css" href="/mobilebrowsergame/css/stylesheet.css" />

Categories