• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Rewrite rules and Virtual Hosts not working

Hi Guys,
I've got an ubunut webserver running apache2. My website doc's were located in /var/www but recently i wanted to host another website on that server.

I modified /etc/apache2/apache2.conf to

NameVirtualHost 111.111.111.111:80

Open in new window

To setup name based hosting.

I then created a new file in /var/apache2/sites-enabled  with the following
<VirtualHost 111.111.111.111:80>
	ServerName firstsite.com.au
	ServerAlias www.firstsite.com.au.com
	ServerAdmin myemail@gmail.com
        DocumentRoot /var/www/firstsite.com.au

<Directory />
	Allowoverride All
</Directory>

<Directory /var/www/firstsite.com.au>
		Options FollowSymLinks
		AllowOverride All
	</Directory>

</VirtualHost>  

Open in new window


Once that was complete, I created a directory : /var/www/firstsite.com.au
and moved all my website files in there...

There is a .htaccess file in there that deals with rewriting. I didn't write the file so I'm not entirely sure how it works (i have alot of trouble understanding rewriting) but I have a feeling this is what is causing me greif.
This is the contents of it. It is now located in /var/www/firstsite.com.au
Options +FollowSymLinks
#Options -Indexes

# Error Files
ErrorDocument 400 /errors/400.html
ErrorDocument 401 /errors/401.html
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www.firstsite.com.au$ [NC]
RewriteRule ^(.*)$ http://www.firstsite.com.au/$1 [R=301,L]
RewriteRule ^(search.php)/(.*)/(.*)/(.*)$ http://www.firstsite.com.au/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Open in new window


I did place a link between sites-enabled and sites-available for the new firstsite.com.au file that I created in sites-enabled.
also, there is a file called 'default' that is still in sites-enabled and is in sites-available it looks like this
NameVirtualHost *
<VirtualHost *>
	ServerAdmin webmaster@localhost
	
	DocumentRoot /var/www
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
		# This directive allows us to have apache2's default start page
                # in /apache2-default/, but still have / go to the right place
                #RedirectMatch ^/$ /apache2-default/
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog /var/log/apache2/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /var/log/apache2/access.log combined
	ServerSignature On

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

Open in new window



So when i go to firstsite.com.au it displays the splash page, but when i go to any other index
firstsite.com.au/home which should rewrite...its not working..

Anyone able to help?
Cheers
0
doyle007
Asked:
doyle007
  • 11
  • 7
  • 4
2 Solutions
 
pritamduttCommented:
I am not sure what you all you intend to do but I am surely guide you to a excellent .htaccess file generator tool.

Please visit : http://cooletips.de/htaccess/

Hope this help you create and recreate .htaccess files time and again.
0
 
doyle007Author Commented:
My errorlog is full of this when I try to access the site.
This works www.firstsite.com.au (that displays as its spose to) but any other part of the site is not working....Thanks for the link, it looks good, i'll have to have a play with it, but for now..need to get this working :(


[Fri Oct 14 16:19:41 2011] [client 111.111.111.111] File does not exist: /htdocs
[Fri Oct 14 16:32:38 2011] [client 111.111.111.111] File does not exist: /htdocs
0
 
pritamduttCommented:
I don't see reference to /htdocs in the configuration file you have uploaded.

Can you upload the output of apache2 - t -D DUMP_VHOSTS

Thanks!
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
AegilCommented:
I would move the .htacess to .htaccess.old then if you create a php.php file with the following in it:

<?php
phpinfo();
?>

Set the file permissions to 775 for the file.

then try firstsite.com.au/php.php

This would rule out any htaccess file problems and site configuration issues.

If that works fine then we can look through your code. it might be that your site is configured to use a htdocs folder.
0
 
doyle007Author Commented:
Ok, So I renamed .htaccess to .htaccess_old and created a php file and chmoded'd to 775.

When I go to the site/php.php it says Not found..
In the error log it says this

[Fri Oct 14 20:10:49 2011] [error] [client 115.64.165.224] File does not exist: /htdocs
[Fri Oct 14 20:10:49 2011] [debug] mod_deflate.c(615): [client 115.64.165.224] Zlib: Compressed 292 to 221 : URL /php.php

Open in new window


I tried the dump vhosts command...dont think it worked, got this error..

apache2: bad user name ${APACHE_RUN_USER}

Open in new window


I ran this as sudo...

0
 
AegilCommented:
In apache2.conf replace the ${APACHE_RUN_USER} with the www-data as below.
On my server I commented it out.

Then you should be able to run theapache2 - t -D DUMP_VHOSTS as recommended above and we can get some more information on your problem.




#User ${APACHE_RUN_USER}
User www-data
#Group ${APACHE_RUN_GROUP}
Group www-data


0
 
doyle007Author Commented:
Ok, I did that .
Weird becuase in the config file it says to set those users in the envvars which they were..
Oh well I did that and it worked..
here is the output.. (for ease of undertanding, I've unhiden the name of the server from you guys...  :) I duno why i was so paranoid! [firstsite.com.au is really autohunter.com.au)
and yes, there is is SSL...


VirtualHost configuration:
202.2.95.186:80        is a NameVirtualHost
         default server autohunter.com.au (/etc/apache2/sites-enabled/autohunter.com.au:1)
         port 80 namevhost autohunter.com.au (/etc/apache2/sites-enabled/autohunter.com.au:1)
wildcard NameVirtualHosts and _default_ servers:
*:443                  is a NameVirtualHost
         default server autohunter.com.au (/etc/apache2/sites-enabled/ssl:2)
         port 443 namevhost autohunter.com.au (/etc/apache2/sites-enabled/ssl:2)
Syntax OK

Open in new window

0
 
AegilCommented:
Hey,

I notice that https works but http does not, are the files in the same location and are you using the same.

Do you have other http sites on that server. you could set it up with a wildcard to test the site. e.g VirtualHost *:80
Also, can you check apache2.conf and httpd.conf to see if there is a htdocs setting set in there.
0
 
pritamduttCommented:
Yeah!! Even I was wondering that! Please check your configuration files.

Why don't you run the following commands to see where are DocumentRoots are configured.
grep -i 'DocumentRoot' httpd.conf
grep -i 'DocumentRoot' apache2.conf 

Open in new window


Also, run it on other included configuration files...
0
 
pritamduttCommented:
Just as a thought it is possible the Virtual Host configuration is not working for some reason, and the _default_ configuration must be set to /htdocs folder.

Please check!
0
 
doyle007Author Commented:
Yeah...thats really odd. I never tested it with https....strange...

here is my apache2.conf

#
# Based upon the NCSA server configuration files originally by Rob McCool.
#
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.2/ for detailed information about
# the directives.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure
# consult the online docs. You have been warned.  
#
# The configuration directives are grouped into three basic sections:
#  1. Directives that control the operation of the Apache server process as a
#     whole (the 'global environment').
#  2. Directives that define the parameters of the 'main' or 'default' server,
#     which responds to requests that aren't handled by a virtual host.
#     These directives also provide default values for the settings
#     of all virtual hosts.
#  3. Settings for virtual hosts, which allow Web requests to be sent to
#     different IP addresses or hostnames and have them handled by the
#     same Apache server process.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path.  If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so "/var/log/apache2/foo.log"
# with ServerRoot set to "" will be interpreted by the
# server as "//var/log/apache2/foo.log".
#

### Section 1: Global Environment
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests it can handle or where it
# can find its configuration files.
#

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE!  If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the LockFile documentation (available
# at <URL:http://httpd.apache.org/docs-2.1/mod/mpm_common.html#lockfile>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
ServerRoot "/etc/apache2"

#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
#<IfModule !mpm_winnt.c>
#<IfModule !mpm_netware.c>
LockFile /var/lock/apache2/accept.lock
#</IfModule>
#</IfModule>

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15

##
## Server-Pool Size Regulation (MPM specific)
## 

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# event MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_event_module>
    StartServers          2
    MaxClients          150
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxRequestsPerChild   0
</IfModule>

# These need to be set in /etc/apache2/envvars

#User ${APACHE_RUN_USER}
User www-data
#Group ${APACHE_RUN_GROUP}
Group www-data


#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#

AccessFileName .htaccess

#
# The following lines prevent .htaccess and .htpasswd files from being 
# viewed by Web clients. 
#
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

#
# DefaultType is the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType text/plain


#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog /var/log/apache2/error.log

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel debug

# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf

# Include all the user configurations:
Include /etc/apache2/httpd.conf

# Include ports listing
Include /etc/apache2/ports.conf

#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
# If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
#
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

#
# Define an access log for VirtualHosts that don't define their own logfile
CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined


# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
Include /etc/apache2/conf.d/

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/

#added by me 13/10/2011 10:18PM
NameVirtualHost 202.2.95.186:80

Open in new window


The result of those grep's returned nothing. I also grep'd for htdocs...nothing.
This is a real mystery.
Anything else I can try...
0
 
doyle007Author Commented:
here is my ports.conf...

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz


#commented by Sameh on 13/09/2011 11.54pm --this is listed already in apache2.conf
#NameVirtualHost *:80
Listen 80

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

Open in new window

0
 
pritamduttCommented:
Please see line 237 and 240 of the config file, you see other configurations files are included from


# Include generic snippets of statements
Include /etc/apache2/conf.d/

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/

Please check these directories for other *.conf files
Regards.
0
 
doyle007Author Commented:
Ok I tried something.
I changed the default filein the sites-enabled folder from this

NameVirtualHost *
<VirtualHost *>
	ServerAdmin webmaster@localhost
...........

Open in new window


to this ------ Added the :80

NameVirtualHost *:80
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
...........

Open in new window


and now when you go to autohunter.com.au/php.php it switches to https....

uhmm...why> lol

0
 
AegilCommented:
http://www.autohunter.com.au/autohunter.com.au/php.php

The above seems to work at the moment and http://autohunter.com.au is showing the domains directory. I think you need to check the Document root now, and make sure its got the full path in to that directory. As it seems to be using /var/www as document root at the moment
0
 
doyle007Author Commented:
Ok..
I've checked those folders out too...

/etc/apache2/conf.d/ contains the following
-rw-r--r-- 1 root root 1481 2010-11-19 08:18 security
-rw-r--r-- 1 root root 3296 2010-11-19 08:18 localized-error-pages
-rw-r--r-- 1 root root  269 2010-11-19 08:18 charset
drwxr-xr-x 7 root root 4096 2011-06-23 13:28 ..
lrwxrwxrwx 1 root root   45 2011-06-24 23:08 javascript-common.conf -> /etc/javascript-common/javascript-common.conf
lrwxrwxrwx 1 root root   28 2011-06-24 23:09 phpmyadmin.conf -> ../../phpmyadmin/apache.conf
drwxr-xr-x 2 root root 4096 2011-06-24 23:09 .

Open in new window


when I checked whats in phpmyadmin.conf it has this

# phpMyAdmin default Apache configuration

Alias /phpmyadmin /usr/share/phpmyadmin

<Directory /usr/share/phpmyadmin>
	Options FollowSymLinks
	DirectoryIndex index.php

	<IfModule mod_php5.c>
		AddType application/x-httpd-php .php

		php_flag magic_quotes_gpc Off
		php_flag track_vars On
		php_flag register_globals Off
		php_value include_path .
	</IfModule>

</Directory>

# Authorize for setup
<Directory /usr/share/phpmyadmin/setup>
    <IfModule mod_authn_file.c>
    AuthType Basic
    AuthName "phpMyAdmin Setup"
    AuthUserFile /etc/phpmyadmin/htpasswd.setup
    </IfModule>
    Require valid-user
</Directory>

# Disallow web access to directories that don't need it
<Directory /usr/share/phpmyadmin/libraries>
    Order Deny,Allow
    Deny from All
</Directory>
<Directory /usr/share/phpmyadmin/setup/lib>
    Order Deny,Allow
    Deny from All
</Directory>

Open in new window


and the other directory [sites-

lrwxrwxrwx 1 root root   22 2011-06-22 01:06 ssl -> ../sites-available/ssl
drwxr-xr-x 7 root root 4096 2011-06-23 13:28 ..
lrwxrwxrwx 1 root root   36 2011-10-13 23:08 autohunter.com.au -> ../sites-available/autohunter.com.au
lrwxrwxrwx 1 root root   26 2011-10-14 21:40 default -> ../sites-available/default
drwxr-xr-x 2 root root 4096 2011-10-14 21:40 .

Open in new window

0
 
pritamduttCommented:
Please check the contents of default
0
 
doyle007Author Commented:
I did a recursive grep to see where document root is mentioned..


root@autohunter:/etc# grep -r "DocumentRoot" .
./apache2/sites-enabled/ssl:       DocumentRoot /var/www/autohunter.com.au
./apache2/sites-enabled/autohunter.com.au:        DocumentRoot /var/www/autohunter.com.au
./apache2/sites-enabled/default:	DocumentRoot /var/www
./apache2/sites-available/ssl:       DocumentRoot /var/www/autohunter.com.au
./apache2/sites-available/autohunter.com.au:        DocumentRoot /var/www/autohunter.com.au
./apache2/sites-available/default:	DocumentRoot /var/www

Open in new window

0
 
doyle007Author Commented:
Contents of /var/apache2/sites-enabled/default
...Do I need this file?


NameVirtualHost *:80
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	
	DocumentRoot /var/www
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
		# This directive allows us to have apache2's default start page
                # in /apache2-default/, but still have / go to the right place
                #RedirectMatch ^/$ /apache2-default/
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog /var/log/apache2/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /var/log/apache2/access.log combined
	ServerSignature On

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Open in new window

0
 
pritamduttCommented:
Finally I am able to access the website on the http port.. also the automatic redirection to
1. Non WWW to WWW is working
2. Redirection to Login Page is working..



Cheers!!

Finally worked!!
0
 
doyle007Author Commented:
Thanks guys,
The fix was the following.

Remove the NameVirtualHost 202.2.95.186:80 from apache2.conf
and set it to

NameVirtualHost *:80

Then I had to change the files i built in sites-available to be like this
<NameVirtualHost *:80>

instead of teh name with the IP address...
Then it worked. Thats all it was...

All the instructions over the net say to use the IP of the server as the namevirtualhost...

Thanks for your help guys. All my virtualhosts are now working on the site. Cheers.


 
0
 
doyle007Author Commented:
Both guys helped me into looking in different places to find the issue. Cheers.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now