Page 1 of 1

Root-relative URL doesn't work locally

PostPosted: 22. May 2018 18:24
by FrankC
Hi,

A link like
Code: Select all
<script src="/x-files/pretext-script.js"></script>
works fine on the remote server, but on the local server I get a 404:
Code: Select all
Loading failed for the <script> with source “http://localhost/x-files/pretext-script.js”


The latter location is indeed not where the x-files folder is at (it is at C:\xampp\htdocs\www\Websites\NameOfWebsite\), but I would like to use the root-relative URL (the leading slash). Is there a XAMPP setting that I can change so that I can? Or does a newer XAMPP version solve this problem?

Re: Root-relative URL doesn't work locally

PostPosted: 22. May 2018 19:20
by Altrea
Hi,

What is the DocumentRoot of localhost?
urls starting with a slash (/) are always based on the DocumentRoot used for that request.
This is unrelated of the XAMPP version.

Re: Root-relative URL doesn't work locally

PostPosted: 22. May 2018 21:02
by Nobbie
FrankC wrote: Is there a XAMPP setting that I can change so that I can?


Of course there is no "XAMPP setting", as XAMPP is only a distribution, not a software.

But there maybe an Apache setting which helps you out of that, probably "ALIAS" might solve your problem. I really wonder that you are still struggeling with very basic Apache knowlegde, after so many years? Why is that? Dont you ever read any documentation, or what is wrong? I really cant get it, i have become a real "professional" in all these years and definately started from scratch without any knowlegde about Apache, PHP and MySQL. And its definately not because i am a genious (i wish i were), far from honestly. Simply reading documentation and trying out. You joined 7(!!!) years ago and you are still asking low level beginner questions. Very weird.

Re: Root-relative URL doesn't work locally

PostPosted: 23. May 2018 01:20
by FrankC
Altrea wrote:What is the DocumentRoot of localhost?

That's C:\xampp\htdocs\www\Websites\NameOfWebsite\ .

Nobbie wrote:You joined 7(!!!) years ago and you are still asking low level beginner questions. Very weird.

Because I have never needed root-relative URLs before. I always managed with file-relative URLs, which do work on both the remote and local server. And may I say that I do not exactly feel like an appreciated XAMPP user by your comment.

Re: Root-relative URL doesn't work locally

PostPosted: 25. May 2018 08:39
by steve_t
One solution is to use Apache virtual hosts. For the AF pre-provided info on Apache virtual hosts, see XAMPP Dashboard | HOW-TO Guides | Configure Virtual Hosts.

Using root relative paths means you only have to provide the path (with leading slash) after the root of the web server. For XAMPP, the root of the Apache web server is C:/xampp/htdocs/. However, in your local, the root your web site(s) are C:/xampp/htdocs/www/Websites/NameOfWebsite/, which is not the root of the XAMPP Apache web server, which is why your root relative paths are not working locally.

In short, to use Apache virtual hosts as solution, in the Apache httpd-vhosts.conf file, copy one of the <VirtualHost *:80></VirtualHost> sections, make sure it is not commented out, and (in your case) set DocumentRoot to "C:/xampp/htdocs/www/Websites/NameOfWebsite/", which indicates the path to use as root relative path. Then set ServerName to http://www.websitedomain.com, which indicates the domain name to "register" to the DocumentRoot. Lastly, in the Windows\System32\drivers\etc\hosts file, add line 127.0.0.1 http://www.websitedomain.com and make sure it is not commented out. This DNSs/resolves the domain name entered into the web browser to 127.0.0.1/localhost. If necessary, restart XAMPP Apache to use new httpd-vhosts.conf, and restart web browser to use new hosts file.

Play around with virtual hosts. It works. Moreover, it scales to use one XAMPP installation to support the development of multiple web sites locally as follows. In the Apache httpd-vhosts.conf file, register each root relative path to its domain name (all uncommented out). In the Windows hosts file, DNS/resolve each domain name to 127.0.0.1/localhost, all commented out except the one that you want to use currently/presently locally. Please re-read that last line. Again, if necessary, restart XAMPP Apache to use new httpd-vhosts.conf, and restart web browser to use new hosts file.

Hope it helps.

Steve

Re: Root-relative URL doesn't work locally

PostPosted: 25. May 2018 09:01
by Altrea
FrankC wrote:
Altrea wrote:What is the DocumentRoot of localhost?

That's C:\xampp\htdocs\www\Websites\NameOfWebsite\ .

Okay, and what is the full path of the file /x-files/pretext-script.js?

Re: Root-relative URL doesn't work locally

PostPosted: 25. May 2018 11:46
by FrankC
@Steve -- I figured as much, that it would be a matter of creating virtual hosts (VH). But does that creation process really have to be that tricky? Installing Wordpress is even easier, with Bitnamy. And the How To describes a VH for one Wordpress site, while I have multiple normal sites. Also, the How To guide is well hidden. A beginner will never find it.

I'd think it would be a great selling point if AF would (semi-)automate the creation process. At least the documentation should cover multiple normal sites and be easier to find.

Sorry, in post above, phpBB converted domain names to URLs

PostPosted: 26. May 2018 05:15
by steve_t
Sorry, in post above, phpBB default converts domain names (correct) to URLs (incorrect). The corrected sentences are below:

Then set ServerName to www.websitedomain.com, which indicates the domain name to "register" to the DocumentRoot. Lastly, in the Windows\System32\drivers\etc\hosts file, add line 127.0.0.1 www.websitedomain.com and make sure it is not commented out.

Re: Root-relative URL doesn't work locally

PostPosted: 26. May 2018 05:45
by steve_t
FrankC wrote:But does that creation process really have to be that tricky? Installing Wordpress is even easier, with Bitnamy. And the How To describes a VH for one Wordpress site, while I have multiple normal sites. Also, the How To guide is well hidden. A beginner will never find it.

I'd think it would be a great selling point if AF would (semi-)automate the creation process. At least the documentation should cover multiple normal sites and be easier to find.


If you think about it, how is the XAMPP Apache installer supposed to predict the DocumentRoot path that someone wants to use? Remember, the C:\xampp\htdocs directory does not even exist until after XAMPP is installed. So, configuring virtual hosts cannot be done by/built into the XAMPP Apache installer. Besides, configuring virtual hosts is simply editing two files. For the other concerns, the AF virtual hosts documentation seems sufficient, and sufficiently easy to find, to me.

Steve

Re: Root-relative URL doesn't work locally

PostPosted: 02. June 2018 01:36
by FrankC
Regarding the difficulties that arise when using test tablets and phones in combination with Virtual Hosts (viewtopic.php?f=16&t=77009), I'll just keep using file-relative URLs. Much easier for me, and my projects function well with them, too.

Re: Root-relative URL doesn't work locally

PostPosted: 02. June 2018 09:55
by Nobbie
As the URLs do not provide different servers, VirtualHosts wouldnt solve that issue anyway. I still think that a simple ALIAS does the job, as i already mentioned above.