.htaccess and links on localhost (Sufficiently Resolved)

Problems with the Windows version of XAMPP, questions, comments, and anything related.

.htaccess and links on localhost (Sufficiently Resolved)

Postby KallistaAEnvarou » 04. December 2007 18:49

OK, so, as I'm sure most of you are doing, I'm modifying my websites on my localhost. I have them under a folder to separate the website files from the XAMPP files. Now, I noticed that, since $_SERVER[HTTP_HOST] is localhost, all my links go to localhost/link_name, versus localhost/folder/link_name but ONLY when I'm using localhost, since I obviously don't want to switch anything when I go to my live server.

I've been trying to research how to accommodate so that I don't have to manually change the URLS with an if statement, but I can't seem to figure out how. I'm guessing the only way is to use .htaccess, but I can't find the code. I know I can do <a href="linkname">, but I hate that syntax as well, mainly because I have an include file that, even in the subdirectories, writes links to the main directory. I prefer to do <a href="/linkname">.

I have another folder for another website that I have, so I don't want to change the host name in http.conf. What are my options? I've tried looking this stuff up on Google, but the only answers I'm getting are for removing the www from the domain name.

[Edited to make paragraphs]
Last edited by KallistaAEnvarou on 07. December 2007 23:26, edited 2 times in total.
KallistaAEnvarou
 
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California

Postby Izzy » 05. December 2007 01:15

Have you tried/looked at creating virtualhost containers to separate your web content from the XAMPP localhost files?

xampp\apache\conf\extra\httpd.vhosts.conf

A search in the Xampp for Windows forum, category Apache Friends in English, using the keyword vhost or virtualhost will give some clues and example solutions - you can also add Izzy in the Search for Author field if you wish to narrow the search a little.

The Apache related docs.
http://httpd.apache.org/docs/2.3/vhosts/
http://httpd.apache.org/docs/2.3/vhosts/examples.html
http://httpd.apache.org/docs/2.3/mod/core.html

Tip
Your post is hard to read when all on one line.

Try and split up your posts into separate paragraphs (lines) with a break in between for ease of reading.


===========================================
1. The new DeskTopXampp Launch Control for XAMPP / XAMPPlite
Posted by Ridgewood available from Ridgewood'sDTX web site

2. Build Rich AJAX Applications - Faster
TIBCO General Interface Pro Edition but FREE and Open Source
Fully working with NO donations required to get a user/password
===========================================
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Postby KallistaAEnvarou » 05. December 2007 03:20

Sorry about that. I was trying to fix my links and change a bunch of my functions to pass by reference versus returning, so I didn't have time to think clearly.

Anyway, pretty much what I want to do is this:

http://localhost/website1 -->all <a href="/linkname"> links go to http://localhost/website1

http://localhost/website2 -->all <a href="/linkname"> links go to http://localhost/website2

I want to keep them in their respective folders to keep them organized. Will those links do what I want? (Sorry, I'm at school, so I don't have time to go over them carefully).
KallistaAEnvarou
 
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California

Postby Izzy » 05. December 2007 05:53

That's OK, and no problem for me, but some will skip a post they have difficulty reading.

Some reading about Document relative paths, root-relative paths and absolute paths which is what this subject matter refers.

http://www.communitymx.com/content/arti ... ?cid=230ad
http://webdesign.about.com/od/beginning ... 40502a.htm
http://kb.adobe.com/selfservice/viewCon ... &sliceId=1
http://en.wikipedia.org/wiki/Path_(computing)


With 3 websites you should seriously consider using the file
xampp\apache\conf\extra\httpd-vhost.conf

For an example for your situation I put together this:
Code: Select all
NameVirtualHost *:80

<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

<VirtualHost *:80>
ServerName website1
ServerAlias www.website1
ServerAdmin serveradmin@website1
DocumentRoot "C:/xampp/htdocs/website1"
<Directory "C:/xampp/htdocs/website1" >
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ "C:/xampp/htdocs/website1/cgi-bin/"
</VirtualHost>

<VirtualHost *:80>
ServerName website2
ServerAlias www.website2
ServerAdmin serveradmin@website1
DocumentRoot "C:/xampp/htdocs/website2"
<Directory "C:/xampp/htdocs/website2" >
Options Indexes +FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ "C:/xampp/htdocs/website2/cgi-bin/"
</VirtualHost>


The Windows host file
C:\WINDOWS\system32\drivers\etc\host
needs to be edited by dragging it into your text editor
Code: Select all
127.0.0.1 localhost
127.0.0.1 website1
127.0.0.1 website2


HTH


===========================================
1. The new DeskTopXampp Launch Control for XAMPP / XAMPPlite
Posted by Ridgewood available from Ridgewood'sDTX web site

2. Build Rich AJAX Applications - Faster
TIBCO General Interface Pro Edition but FREE and Open Source
Fully working with NO donations required to get a user/password
===========================================
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Postby KallistaAEnvarou » 05. December 2007 16:23

That's OK, and no problem for me, but some will skip a post they have difficulty reading.

I know. I'm one of them. Anyway...

I'll take a look at those sites and do a search. If I still have trouble, I'll let you know.

Thanks.

Oh also, I'm not hosting them from my computer. I use Bluehost to host them live.
KallistaAEnvarou
 
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California

Postby KallistaAEnvarou » 07. December 2007 17:06

OK, that didn't work. I did the VirtualHost thing just fine, and I got around Vista's inability to tell the truth about what it will and won't allow to happen so I could overwrite the hosts file, but I'm still not seeing any server link changes. I'll be honest that I haven't done the search yet since I've been kinda busy. I did read the links, and I already knew about all that stuff, though I didn't know the exact names. I want to use root-relative paths since, as I said before, I want to be able to use the links globally for my sub-folders.

Here are the copies of the latest attempts on my exact files.

c:\xampp\apache\conf\extra\httpd-vhost.conf
Code: Select all
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
##NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host.example.com
##    DocumentRoot /www/docs/dummy-host.example.com
##    ServerName dummy-host.example.com
##    ServerAlias www.dummy-host.example.com
##    ErrorLog @rel_logfiledir@/dummy-host.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host.example.com-access_log common
##</VirtualHost>

##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
##    DocumentRoot /www/docs/dummy-host2.example.com
##    ServerName dummy-host2.example.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
##</VirtualHost>

NameVirtualHost *:80

<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

<VirtualHost *:80>
ServerName localhost/phplearningneeds
ServerAlias localhost/phplearningneeds
ServerAdmin webmaster@phplearningneeds.com
DocumentRoot "C:/xampp/htdocs/phplearningneeds"
<Directory "C:/xampp/htdocs/phplearningneeds">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ "C:/xampp/htdocs/phplearningneeds/cgi-bin/"
</VirtualHost>

<VirtualHost *:80>
ServerName localhost/reportingscience
ServerAlias localhost/reportingscience
ServerAdmin webmaster@reportingscience.com
DocumentRoot "C:/xampp/htdocs/reportingscience"
<Directory "C:/xampp/htdocs/reportingscience">
Options Indexes +FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ "C:/xampp/htdocs/reportingscience/cgi-bin/"
</VirtualHost>


c:\windows\system32\drivers\etc\hosts
Code: Select all
# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1   localhost
127.0.0.1   phplearningneeds.com
127.0.0.1   reportingscience.com
::1             localhost


And now, here are my server variables, identical to what they were before.
Code: Select all
$_SERVER
        REDIRECT_STATUS='200'
        HTTP_USER_AGENT='Opera/9.24 (Windows NT 6.0; U; en)'
        HTTP_HOST='localhost'
        HTTP_ACCEPT='text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1'
        HTTP_ACCEPT_LANGUAGE='en-US,en;q=0.9'
        HTTP_ACCEPT_CHARSET='iso-8859-1, utf-8, utf-16, *;q=0.1'
        HTTP_ACCEPT_ENCODING='deflate, gzip, x-gzip, identity, *;q=0'
        HTTP_COOKIE='PHPSESSID=a2da5181f23556538dacd7ebcc0f7047'
        HTTP_COOKIE2='$Version=1'
        HTTP_CONNECTION='Keep-Alive, TE'
        HTTP_TE='deflate, gzip, chunked, identity, trailers'
        PATH='C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;'
        SystemRoot='C:\Windows'
        COMSPEC='C:\Windows\system32\cmd.exe'
        PATHEXT='.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'
        WINDIR='C:\Windows'
        SERVER_SIGNATURE='
Apache/2.2.6 (Win32) DAV/2 mod_ssl/2.2.6 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.4 Server at localhost Port 80
'
        SERVER_SOFTWARE='Apache/2.2.6 (Win32) DAV/2 mod_ssl/2.2.6 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.4'
        SERVER_NAME='localhost'
        SERVER_ADDR='127.0.0.1'
        SERVER_PORT='80'
        REMOTE_ADDR='127.0.0.1'
        DOCUMENT_ROOT='C:/xampp/htdocs'
        SERVER_ADMIN='admin@localhost'
        SCRIPT_FILENAME='C:/xampp/htdocs/reportingscience/index.php'
        REMOTE_PORT='51279'
        REDIRECT_URL='/reportingscience/'
        GATEWAY_INTERFACE='CGI/1.1'
        SERVER_PROTOCOL='HTTP/1.1'
        REQUEST_METHOD='GET'
        QUERY_STRING=''
        REQUEST_URI='/reportingscience/'
        SCRIPT_NAME='/reportingscience/index.php'
        PHP_SELF='/reportingscience/index.php'
        REQUEST_TIME='1197047966'
        argv
        argc='0'


So...what am I doing wrong? I've even tried adding a cgi-bin folder, but that didn't work.
KallistaAEnvarou
 
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California

Postby Izzy » 07. December 2007 21:57

This is incorrect and will fail.
ServerName localhost/phplearningneeds
ServerAlias localhost/phplearningneeds

Should be the same as you have indicated in your Windows hosts file.
ServerName phplearningneeds.com
ServerAlias www.phplearningneeds.com

Now it is available at these addresses (locally - not from the Internet unless you have registered the domain name etc.) because the server knows to look for phplearningneeds.com at the localhost location you specified in the DocumentRoot:
"C:/xampp/htdocs/phplearningneeds"

http://phplearningneeds.com/index.php
http://www.phplearningneeds.com/index.php
http://localhost/phplearningneeds/index.php
http://127.0.0.1/phplearningneeds/index.php

Code: Select all
NameVirtualHost *:80

<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

<VirtualHost *:80>
ServerName phplearningneeds.com
ServerAlias phplearningneeds.com
ServerAdmin webmaster@phplearningneeds.com
DocumentRoot "C:/xampp/htdocs/phplearningneeds"
<Directory "C:/xampp/htdocs/phplearningneeds">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ "C:/xampp/htdocs/phplearningneeds/cgi-bin/"
</VirtualHost>

<VirtualHost *:80>
ServerName reportingscience.com
ServerAlias reportingscience.com
ServerAdmin webmaster@reportingscience.com
DocumentRoot "C:/xampp/htdocs/reportingscience"
<Directory "C:/xampp/htdocs/reportingscience">
Options Indexes +FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ "C:/xampp/htdocs/reportingscience/cgi-bin/"
</VirtualHost>



===========================================
1. The new DeskTopXampp Launch Control for XAMPP / XAMPPlite
Posted by Ridgewood available from Ridgewood'sDTX web site

2. Build Rich AJAX Applications - Faster
TIBCO General Interface Pro Edition but FREE and Open Source
Fully working with NO donations required to get a user/password
===========================================
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Postby KallistaAEnvarou » 07. December 2007 22:04

I'd rather not type the URL's in like that, because I do have those web addresses purchased and want to distinguish between my localhost and the live server versions.

I was successful for some reason the last time I went to copy your way (but not before), but I'd rather not for the reasons noted above. I switched to

127.0.0.1 localhost/reportingscience
127.0.0.1 localhost/phplearningneeds

in the hosts file, but that didn't work. Can I get what I want without giving a domain name?
KallistaAEnvarou
 
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California

Postby Izzy » 07. December 2007 23:06

127.0.0.1 reportingscience
127.0.0.1 phplearningneeds

<VirtualHost *:80>
ServerName phplearningneeds
ServerAdmin webmaster@phplearningneeds


<VirtualHost *:80>
ServerName reportingscience
ServerAdmin webmaster@reportingscience

No need for a ServerAlias in your case - read what a ServerAlias Directive is before you use it.

localhost/reportingscience - this is a URI not a ServerName and as I posted before, will fail - read what a ServerName is before using it.

Open a Command Console in xampp\apache\bin and at the prompt issue the command apache -S to verify the integrity of the httpd-vhost.conf file - which is clearly indicated in the readable sections of the httpd-vhost.conf file.

It seems to me you don't want to read the Apache Docs to find out the correct methods and directives to use, which will inevitably result in the simple mistakes you are making and consequently waste a lot of peoples time including your own.



===========================================
1. The new DeskTopXampp Launch Control for XAMPP / XAMPPlite
Posted by Ridgewood available from Ridgewood'sDTX web site

2. Build Rich AJAX Applications - Faster
TIBCO General Interface Pro Edition but FREE and Open Source
Fully working with NO donations required to get a user/password
===========================================
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Postby KallistaAEnvarou » 07. December 2007 23:25

Nah, I've read them. I know that a ServerAlias is, as quoted, "Alternate names for a host used when matching requests to name-virtual hosts". I'm new to localhost servers, so I didn't know what that meant. I'm still not quite sure even though everything is working correctly now.

Anyway, I've fixed mine up in a compromised way. I'm going to use local_websitename for the Server Names, since apparently the Aliases don't do anything...or do they?. Anyway, thanks a bunch. You're awesome.
KallistaAEnvarou
 
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California

Postby Izzy » 08. December 2007 01:10

Thanks for your responses but it is not I that is awesome but XAMPP and the way the developers have compiled and configured the components in an easy to use combination.

This usually is very difficult if one tries to download, compile and install the individual components and then hopefully get them all to work together.



Now that we have the vhost part 'sufficiently resolved', lets look at the .htaccess aspect of your original post so you can see what you may be able to do as an alternative, perhaps.

This page has some very useful info that you may be able to easily grab a handle on.
http://httpd.apache.org/docs/2.3/mod/mod_alias.html

Check out all the menu links referred to for an understanding of what is possible, including 'mod_rewrite' and 'Mapping URLs to the filesystem'.
http://httpd.apache.org/docs/2.3/misc/rewriteguide.html

Here is a handy tutorial on .htaccess:
http://httpd.apache.org/docs/2.3/howto/htaccess.html


Even if you don't use it for your current path issues, the reading will definitely broaden your understanding of what can be achieved using the .htaccess file to change the default behavior of the server on an individual directory basis.

This Google had some related topic results.



BTW ServerAlias are alternative ServerName(s) that all point to the same vhost DocumentRoot if used in a URI.

For example:
ServerName server.tld
ServerAlias www.server.tld server server.server server_server.tld
(etc. as many as you need all on one line each alias separated by a single space)
DocumentRoot "C:/xampp/htdocs"

Another virtual host container is required if the DocumentRoot is different, as in the case of a subdomain.

For example:
ServerName sub.server.tld
ServerAlias www.sub.server.tld server_sub server.sub sevre.sub
(can also cover popular misspelled names in an address)
DocumentRoot "C:/xampp/htdocs/sub"


===========================================
1. The new DeskTopXampp Launch Control for XAMPP / XAMPPlite
Posted by Ridgewood available from Ridgewood'sDTX web site

2. Build Rich AJAX Applications - Faster
TIBCO General Interface Pro Edition but FREE and Open Source
Fully working with NO donations required to get a user/password
===========================================
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Postby KallistaAEnvarou » 08. December 2007 01:29

OK, so you mean the ServerAlias would be like if I were to type php.com I could get localhost_phplearningneeds.com? That'd make sense.

As far as my .htaccess is concerned, I have my .htaccess set up to redirect every page my site encounters to go to the /index.php page. I do that because validator.w3.org can tell if a site has a fake 404 page, which I don't want. Then my index.php page processes everything, and I have headers for images and css files, but the only thing is that I can't figure out how to do headers for JavaScript files. Eh, I'll figure it out eventually.

PS I said you're awesome because you helped me solve my issue. Plus, everybody, even if in a sick way, has a little dose of awesomeness.
KallistaAEnvarou
 
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 126 guests