Fabien Bourigault
Fabien Bourigault

Reputation: 85

Absolute Path in ajax website

I need to create absolute path (css, js, include php, ...).

Instead of having relative path ("../../js/myfile.js"), I would like to have absolute path ("/js/myfile.js").

I'm using MAMP PRO. I access to my website with this URL : http://localhost/mywebsite/index.php

In my index.php file, when I include my javascript files with absolute path "/js/myfile.js", it try to access to this file : "localhost/js/myfile.js" instead of "localhost/mywebsite/myfile.js".

How to secpicify the root folder in my htaccess ?

Thanks a lot,

Upvotes: 0

Views: 203

Answers (1)

David Suraro
David Suraro

Reputation: 21

// Example url
$url = 'example.com/website';

// Parse url use php
$path = parse_url($url, PHP_URL_PATH);

// Return url part to array
$url_parts = explode('/',$path); // $url_parts[1] is your sub directory: website

define('SITE_PATH', '/'.$url_parts[1].'/');

And at your html

<script src="<?php echo SITE_PATH.'js/app.js' ?>"></script>

Upvotes: 1

Related Questions