Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

php - 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:wampwwwproject1 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 wampwwwproject1 style folders.

When doing that the DocumentRoot is wampwww and that is what causes these problems.

These tools expect the DocumentRoot to be the root of the site i.e. wampwwwproject1 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 localhostproject1 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 wampinapacheapache2.4.9confextrahttpd-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 wampinapacheapache2.4.9in 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...