how to define a virtual root? - php

The root of my local server is /Users/myname/Sites and all web projects are in seperate directories in that e.g. /Users/myname/Sites/newproject
The problem I've got is I've got a site which has all html paths written beginning with a slash e.g <a href="/dir/file.txt"> which makes the browser look for the file file.txt in /Users/myname/Sites/ rather than in /Users/myname/Sites/newproject where the file is located.
Rather than edit every single file path to either remove the beginning slash or to put in a php variable at the start I was wondering if there was some easy way to do this site wide?
I've tried using <base href="http://localhost/newproject" />
I've tried setting the DocumentRoot in .htaccess
I've tried setting the $_SERVER['DOCUMENT_ROOT'] to $_SERVER['DOCUMENT_ROOT']."/newproject";
Unfortunately none of these seem to work.

You want to setup a virtual host (if you are using apache) to specify that the / url should map to /User/myname/Sites/newproject.
You would do this like so (edit httpd.conf or place a file in conf.d):
# Listen for virtual host requests on all IP addresses, port 80
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /User/myname/Sites/newproject
ServerName localhost
</VirtualHost>
Note that when you do this, you are essentially "setting document root to be your newproject folder", which will shadow everything else in the Sites folder.
If this is not what you are talking about, please let me know so I can modify my answer.

You can try setting up a virtual host. Assuming from your folder structure that you are on Mac OS X, try this:
http://mark-kirby.co.uk/2008/setting-up-virtual-hosts-on-os-x-leopard

Related

Files in htdocs not showing up (MAMP) PHP

I'm sorry if this has been asked before but I can't seem to find a concrete answer. Within my htdocs folder, I created a folder named Testing. Inside Testing, I placed to two php files named: email_form.php and email_script.php.
When I go on my browser(Chrome), I would type in localhost:8888/Testing in the search bar so that I could see it but I can't. What could I possibly be doing wrong?
I had the same issue and found a solution. Keep in mind I am running on windows 10.
The default port was set to 80 and the url for the start page was just localhost/MAMP
I simply changed the default port in preferences to the usual 8888 and then typed localhost:8888/test/index.html to get my test page up and running. I know its an old thread but hopefully this helps someone
If not one of the files is an index file, you need to add the file name in the URL. A common name for index file is index.php, but it depends on your configuration. In this case the file name in the URL is obsolete.
Additional be sure that php files are processed by your webserver.
While entering the filename in the url and the file is not shown (State 404), then check your config (vhost / virtualhost) settings if the correct document folder is used and if there is a name defined, so that the content will be delivered by using a dns name or maybe by a specific IP only.
<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>

MAMP Virtual host needs www?

I update my /etc/hosts file as
127.0.0.1 www.mysite.com
Then in the MAMP's httpd-vhosts.conf, i updated the ServerName as
ServerName www.mysite.com
DocumentRoot /Applications/MAMP/htdocs/mysite
Now, it's working fine. So when I input mysite.com in the browser, it shows the index.html that is inside my folder /Applications/MAMP/htdocs/mysite.
My question is, when I first check the internet on how to create virtual host in MAMP, the examples never use www. in the MAMP .conf file above. So what I did, I remove the www. in the /etc/hosts file and remove also from ServerName in MAMP's .conf file. Now it doesn't work. So do we really need to add the www. in these files?
Another question is since I want to put the index.html inside /Applications/MAMP/htdocs/mysite/public_html, so what I did is I set the DocumentRoot to /Applications/MAMP/htdocs/mysite/public_html instead. Is this correct? It seems to work. Sorry for the confusion I'm kinda new to this thing.
There is no need do configure the conf file directly. Just use the MAMP interface. "littleyellowbird.com" as domain and in the alias section put "www.littleyellowbird.com".
Mamp rebuilds a conf each time you save the settings, your changes have a good chance to get overwritten.
If you want to get to "littleyellowbird.com" even when you enter "www.littleyellowbird.com" in the browser, use a rewrite rule in the .htaccess file for this domain.
Greetings,
Peter

Laravel 5.1 - Remove "public/" of the URL whith Method bind?

I need some help Please, for I'm beginner with Laravel and MVC.
I want to remove the "public/" of the URL.
The only two "solutions" that I found in Google, are:
Either with .htaccess. But with me (and obviously not with me) it's not working.
Either by putting what is in the "public" folder at the root of the project Laravel. This is not good for security reasons.
There is a real solution to remove the "public/" URL? So that for example this URL:
localhost/Laravel/public/test
Accessible only with this URL:
localhost/Laravel/test
Because if it or has no solution, it is of no use that I continuous to take courses on this Frameworks.
_I had read, that there may be a solution with this in AppServiceProvider:
http://www.creerwebsite.com/medias/upload/larav2.png
Or with this in the Router or a container:
App::bind('path.public', function() {
return base_path().'/public_html';
});
But I am beginner, so I do not find the solution.
Thank you.
The best option is to configure your local installation of WAMP to use Laravel's public folder as the document root. You have two options here:
Option 1. Use wamp just for this project alone
Find your httpd.conf file. This is the server config, and should be located in C:\wamp\bin\apache\Apache2.4.4\conf. (it's a good idea to make a backup of this file before editing)
Open the config file in notepad, find the line DocumentRoot "c:/wamp/www" and change to DocumentRoot "c:/wamp/www/public" (assuming you have your laravel project in the www folder)
save the httpd.conf and restart wamp.
Now when you visit http://localhost you should see the Laravel welcome screen.
Option 2. Set up a virtual host
This option is a bit more complicated, but will allow you to have multiple websites running alongside each other in wamp, each with their own subdomain that you can use to access them in your browser.
1. open the httpd.conf file in notepad and find the line DocumentRoot "c:/wamp/www". Change it to DocumentRoot "c:/wamp/www/default".
2. add the following snippet below this line:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot 'c:/wamp/www'
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName myproject.localhost
DocumentRoot 'c:/wamp/www/myproject/public'
</VirtualHost>
This will create two new virtual hosts running on your local ip. One will work on the domain localhost and serve files from the www/default folder, the other will work on the domain myproject.localhost and serve files from the www/myproject/public folder.
locate your hosts file at C:\Windows\System32\drivers\etc and open it in notepad. add the following lines:
127.0.0.1 localhost
127.0.0.1 myproject.localhost
This will add two domain mappings that point traffic to your local ip for wamp to handle.
Now navigate to the www folder and create C:\wamp\www\default and C:\wamp\www\myproject. Move your laravel project into the \myproject folder ensuring that the public folder is here as well.
finally restart wamp and now you should be able to go to http://myproject.localhost and see your laravel website.
The solution :
_In His Laravel project: Rename the "public" folder like this: "www."
_In An accommodation (example OVH): remove all that is at the root, including the "www" (except: .ovhconfig).
_Send His project on accommodation.
I hope it will not cause conflict. After I think we should also rename in his Laravel the "public/" in "www/".
thank You

WAMP - Remove localhost from project URL

The URLs of my projects in WAMP are not resolving as I'd expect. For example, I'd expect the project in the folder c:\wamp\www\project1 to have the URL http://project1/, but it actually has the URL http://localhost/project1/.
This can cause problems when accessing server variables. How do I fix this?
You can also look at this answer specially if you are now using WAMPServer 3 or greater, for a simple clikc and go way to create Virtual hosts.
Actually this change was intended by the WAMPServer developers and for a good reason.
There is a problem using the localhost/project1 url and the default WAMPServer DocumentRoot in that it causes problems for some frameworks and WordPress type environments, as well as your own code if you are using code which depends on knowing anything about the server environment.
The correct solution is to create Virtual Hosts for all your projects even those that you store in the \wamp\www\project1 style folders.
When doing that the DocumentRoot is \wamp\www and that is what causes these problems.
These tools expect the DocumentRoot to be the root of the site i.e. \wamp\www\project1 so that when they use PHP variables like
$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']
they get the correct answer i.e. the answer they would get on a real live server hosting just that site.
So using the localhost\project1 style url would mean these variables would return
$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www
When they should return
$_SERVER['HTTP_HOST'] = project1
$_SERVER['SERVER_NAME'] = project1
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www/project1
So what you should do to make the My Projects menu work and reduce your pain in copying sites to live servers is:
Create an entry in the HOSTS file for each project like so and remember to create one for access via IPV4(127.0.0.1) and one for access via IPV6 (::1):-
127.0.0.1 localhost
127.0.0.1 project1
::1 localhost
::1 project1
Remember to refresh the Windows DNS Cache after any change to this file like so :-
Start a command window using Run as Administrator and run :-
net stop Dnscache
net start Dnscache
Now you must create a Virtual Host definition, so edit the \wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf file ( apache versions may differ )
Delete the default stuff in there the first time you do this. And then create your Virtual Host definitions like so :-
#
# Use name-based virtual hosting.
# This next line is not required if you are using Apache 2.4.x and should be deleted
NameVirtualHost *:80
## should be first so the wamp menu page loads and is the default site
## should also never be changed from only allowing access from the local machine
## for a bit of extra security from casual ip address probing
<VirtualHost *:80>
DocumentRoot "C:/wamp/www"
ServerName localhost
ServerAlias localhost
<Directory "C:/wamp/www">
AllowOverride All
<IfDefine APACHE24>
Require local
</IfDefine>
<IfDefine !APACHE24>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1 localhost ::1
</IfDefine>
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/project1"
ServerName project1
ServerAlias project1
<Directory "C:/wamp/www/project1">
AllowOverride All
<IfDefine APACHE24>
Require local
</IfDefine>
<IfDefine !APACHE24>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1 localhost ::1
</IfDefine>
</Directory>
</VirtualHost>
Now you need one more change, you must uncomment the line in httpd.conf that includes the above file we have just changed. So edit the httpd.conf file, use the wampmanager menus to do this as it ensures you edit the correct file.
Find this line #Include conf/extra/httpd-vhosts.conf and remove the comment # symbol from the beginning of the line like so :-
Include conf/extra/httpd-vhosts.conf
Now of course you will need to restart Apache so that it picks up your configuration changes.
If Apache does not restart, you probably made a mistake in the config, to find out what is wrong try this.
Open a command window and CD into the \wamp\bin\apache\apache2.4.9\bin folder.
Then run this :-
httpd -t
If the error is in httpd.conf or the httpd-vhost.conf files it will tell you the error and also give you the line number to make finding the error very easy.
in your www folder open index.php
at line 30 change:
$suppress_localhost to be false
this is should look:
$suppress_localhost = false;
In your www folder open index.php at line 30. Here, change $suppress_localhost to be false. So, it is should look:
$suppress_localhost = false;
That was the quickest and easiest fix for me. I'm using 64 bit Wamp.
Okay, I had this problem. So, I troubleshooted the problem and traced it to an actual solution, NOT A HACK.
The SOLUTION :
Right-Click WAMP-server icon
Select WAMP Settings,
Select (enable) option Add localhost in URL
DONE. The remainder is FYI of how and why.
Note: localhost/myproject.php or myproject.php. Although the solution was already accepted, I saw some posts that got me confused. The accepted solution is based on a single project wrt different server applications, based on the OP's specific question, and how to influence that single project in question. But all the other solutions are hacks and don't really answer the solution to the OP's question, but do bring up a good point about the URL. So, according to the other "solutions", here is how to toggle the localhost reference in the URL. Hence my additional solution added to the mix.
This is a toggle switch.
Troubleshooting Process (no hacking involved):
Let's peek at the index.php
Let's look at the config file. Note the variables and Array?
Here is the array. A variable used earlier. Let's see... Oh, it tells us where and what to do.
As noted in the SOLUTION:
Your wamp seems to be configured to run a website on the normally non-existant domain helloworld.
add:
127.0.0.1 helloworld
inside this file: c:\windows\system32\drivers\etc\hosts
Make sure you start your text editor with administrator privileges to be able to edit that file.
This will tell your computer that the otherwise non-existant domain helloworld should be resolved to your loop back address.
For me was the easiest way go to http://localhost and in wampserver homepage use Add a Virtual Host (Tools section). There is nice and easy form to create alias without any problem (instead console when you using tray icon to create alias). No source edit, just using what wamp offers. Remember refresh DNS after creating of alias. Tested on Win10, WampServer 3.0.6 64bit.
Create a virtualhost like RiggsFolly said.
And try to uncomment LoadModule rewrite_module modules/mod_rewrite.so in httpd.conf
HostnameLookups ON not OFF in httpd.conf with DocumentRoot changed or not.
tested in browser for
$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']
C:\wamp\www
In index.php
line 338
($suppress_localhost ? 'http://' : '')
change http:// to http://localhost/
To do this you can create a virtual host using Add a virtual Host utility under Tools menu on localhost's homepage.
For more info on how to create a virtual host visit : Step by step instructions
I think that the easiest and quickest way is to:
Open index.php in your www folder >>> change: $suppress_localhost to
be false / no.
This isn't really an answer per se. It seems that the quickest way of removing a Virtual Host using WAMP is either not create one in the first place or be prepared to uninstall/reinstall it. What is the path to the config file to correct a errant and otherwise not malfunctioning WAMP Server?
If your "Your Projects" folder exists in "wamp/www/" and if you can see the localhost home page after starting wampserever correctly, and still you can't access your projects, then simply go to "wamp/www/" folder, open index.php and search for $suppress_localhost and set its value to false. Restart wampserver, go to localhost and try to access your project.
i also faced same problem after installing new wamp setup on window 7, 64bit.
just change line no. 30
$suppress_localhost = false;
Its work for me.
Open index.php in www folder and set
$suppress_localhost = True;===>$suppress_localhost = false;
that is work.
You can update "urlAddLocalhost" variable in "wamp64/wampmanager.conf" file to on/off. By default it is "off".
My wamp version is 3.0.6.
urlAddLocalhost = "off"

One code on dev server, and production server - how to deal with links?

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.
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.
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__));
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.
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.
http://localhost:81/ for site A
http://localhost:82/ for site B
...
You can either make Apache listen on all IPs and use a different IP for each site.
http://127.0.0.1/ for site A
http://127.0.0.2/ for site B
...
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.
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
Why not use virtual domains within your xampp development for your different clients?

Categories