tags:

views:

75

answers:

6

I have a copy of a code running on the production server, and I use my local machine(s) running xampp as a dev server. I have several websites that I actively develop, so I'm forced to use http://localhost/sitename

All my URLs are relative to the domain, (/file.php). They work fine on the prod server, but on a local server, they all point to localhost, when I want to make them all work relative to the site folder they are in. Is there anything I could do, other than what I do now, which is this:

if($_SERVER['SERVER_NAME'] == "localhost") {
 $path_to = "http://" . $_SERVER['SERVER_NAME'] . "/folder";
 $path_to_files = $_SERVER['DOCUMENT_ROOT'] . "/folder";
} else {
 $path_to = "http://" . $_SERVER['SERVER_NAME'];
 $path_to_files = $_SERVER['DOCUMENT_ROOT'];
}

and simply putting $path_to before each link on the site.

+1  A: 

just dont work with host names in link image/file links.

http goes by root domain/folder/file.jpg => /folder/file.jpg - it doesnt need a http://domain

and script side you can refer to ./ or get your current path with FILE or similar functions/constants:

look into:

basename(dirname(__FILE__));
Tobias
+1  A: 

Why not just use relative links:

<a href='/some/relative/path'>

Then to map filenames to URLs and back, use the relevant $_SERVER vars? (NB for simplicity you don't want too many directory trees associated with each website.

Alternatively just use $_SERVER['HTTP_HOST'] anywhere you need to supply the hostname?

C.

symcbean
His main problem with relative links is that `/foo` will route to `localhost/foo` instead of `localhost/sitename/foo`.
Matt
The make them relative to some abstract datum (or the current file) rather than the webroot - e.g. '../foo'
symcbean
@symcbean - that's not really a valid solution. Dynamic content cannot always know how "deep" it is - think about sites that use URL rewriting. But with a proper setup (e.g. through a vhost), one can always be sure where root truly is, and base link relation on that.
Matt
+5  A: 

You should use a vhost instead.

So, in your hosts file, add something like:

sitename.dev 127.0.0.1

Then in xampp/apache/conf/extra/httpd-vhosts.conf:

<VirtualHost *:80>
        DocumentRoot "/xampp/htdocs/sitename"
        ServerName sitename.dev
</VirtualHost>

Restart Apache then browse to http://sitename.dev

This should provide the same behavior as production.

Read more about vhosts here.

Matt
really cool idea.
Yegor
A: 

Why not use virtual domains within your xampp development for your different clients?

Mark Baker
A: 

The solution you give is a good one, but if you want to hard code absolute URLs, you can configure your dev apache with one virtual host for each site.

See the Apache Virtual Host documentation.

You can either make Apache listen on several ports and assign a port for each site.

You can either make Apache listen on all IPs and use a different IP for each site.

Or you can edit create several hostnames that resolve to 127.0.0.1 and configure name based virtual hosting. You can either use a real DNS server or edit your hosts file, as suggested by Matt.

Artefacto
A: 

What you wanna do is use virtual hosts in Apache. Use this in your httpd.conf:

NameVirtualHost 127.0.0.1

<VirtualHost localhost>
DocumentRoot "C:/My Documents/My Webs"
ServerName localhost
</VirtualHost>

<VirtualHost sitename>
DocumentRoot "C:/My Documents/My Webs/Site Name"
ServerName sitename
</VirtualHost>

Then in your hosts file (C:\Windows\System32\Drivers\etc\hosts on Windows) add the server name:

127.0.0.1        localhost sitename
DisgruntledGoat