Apache redirects www.domain.com to domain.com/domain - redirect

I'm preparing my ubuntu server in Amazon AWS to host a new website. It uses Apache2. I've done this before with no problem whatsoever, for example, to trick a friend I spoofed google.com and www.google.com (editing his hosts file) and everything worked fine with and without 'www.'.
Now I have bought a domain name 'domain.com' with 1&1 , I have configured a subdomain 'www.domain.com' and I have pointed it to my server. The dns record is fine, I checked. I have also edited my sites-enabled default file and added these entries:
<VirtualHost *:80 >
ServerName www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerName domain.com
DocumentRoot /var/www/domain
</VirtualHost>
When I visit domain.com in my browser it works fine, but when I visit www.domain.com I get domain.com/domain/ and obviously a 404.
I used telnet and discovered that the server sends a 301 when you request the root page of host: www.domain.com
I don't know why this is happening, I've tried lots of modifications to the configuration above like:
<VirtualHost *:80 >
ServerAlias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerAlias domain.com
DocumentRoot /var/www/domain
</VirtualHost>
.
<VirtualHost *:80 >
ServerName domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerName www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
.
<VirtualHost *:80 >
ServerName domain.com
Server Alias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
but it kept happening.
For the record, this is what I used for the spoof that still works fine
<VirtualHost *:80 >
ServerName google.com
DocumentRoot /var/www/google
</VirtualHost>
<VirtualHost *:80 >
ServerName www.google.com
DocumentRoot /var/www/google
</VirtualHost>
I've been searching for two days now, but all I find is the same configuration I'm using and no explanation about why this isn't working.
Thanks for your help!
PS: There are no .htaccess files in this directory or in any of the parent directories.

In case anybody enters this question.
I still don't know what was wrong but after moving these configurations around the file they've magically started working normally.
Thanks to everybody who took the time to read the question.

Its better to put
<VirtualHost *:80 >
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
You can't put a space between Server and Alias so I changed to show you
Hope It Works!

Related

Cannot set up multiple virtual hosts via MAMP

I'm trying to set up virtual hosts for the projects in my MAMP htdocs folder. But I cannot create more than one, it seems. If, for example, I have this in my httpd-vhosts.conf file:
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/first-project/public"
ServerName first-project.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/second-project/public"
ServerName second-project.dev
</VirtualHost>
And I then point the browser to second-project.dev, it will show me the contents of first-project.dev. If I remove the v-host entry for first-project, then I can view second-project.dev with no problem. It doesn't like me having more than one entry, for some reason.
I have the following in my /etc/hosts file:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
127.0.0.1 first-project.dev
127.0.0.1 second-project.dev
255.255.255.255 broadcasthost
::1 localhost
Am I missing something? Maybe a setting in httpd.conf?
I found the answer to my own question. When establishing multiple virtual host entries, you need the following line before the entries:
NameVirtualHost *:80
Also, you need to establish the desired directory to replace the default localhost:
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/"
ServerName localhost
</VirtualHost>
My full httpd-vhosts.conf file:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/sampleapp/public/"
ServerName sampleapp.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/simplewebsite/public/"
ServerName simplewebsite.dev
</VirtualHost>

I try to change redirection 301

In the website of my client, there is this vhost:
<VirtualHost *:80>
ServerName website.com
ServerAlias *.website.com
Redirect 301 / https://www.newwebsite.com/
</VirtualHost>
I want to change the redirection 301 to redirect to nothing.
When I updated the vhost. Nothing change when I restart the server.
Someone can help me ?
Thanks a lot.
Create a blank web page.
Redirect to that page:
<VirtualHost *:80>
ServerName website.com
ServerAlias *.website.com
Redirect 301 / https://website.com/blank.html
</VirtualHost>

Ubuntu 14.04 Redirect Domain to Port

I am trying to direct mydomain.com to a specific port. I have run netstat and I know that my code is listening at 0.0.0.0:3000. I have the following in /etc/apache2/sites-available
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName mydomain.com
ServerAlias www.mydomain.com
ProxyPass / http://0.0.0.0:3000/
ProxyPassReverse / http://0.0.0.0:3000/
</VirtualHost>
I was able to get other domains redirected to a subdirectory. When I test using mydomain.com from a browser, I see the default page.

zf2 virtual host loading localhost/xampp

I'm trying to get zf2 running on my xampp.
I have this vhosts.conf file
NameVirtualHost 127.0.0.1:80
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerName zf2-tutorial.localhost
DocumentRoot "C:/xampp/htdocs/zf2-tutorial"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/zf2-tutorial">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
and this host file
127.0.0.1 zf2-tutorial.localhost
Now, when I run zf2-tutorial.localhost, the browser goes to zf2-tutorial.localhost/xampp and shows xampp page.
When I run localhost it goes to localhost/xampp too.
Please help.
Try http://zf2-tutorial.localhost/public
If that works you could change the following entries in your vhosts file
DocumentRoot "C:/xampp/htdocs/zf2-tutorial/public"
<Directory "C:/xampp/htdocs/zf2-tutorial/public">

How to rewrite a domain to a subfolder

I have one local server called server1 with a subdirectory sub (server1/sub/).
Now i have a dns entry which redirects server2 to server1.
I want to configure my apacher server in a way that when I open server2 in a browser I get the content from server1/sub/.
The url should not change to server1/sub/.
Is this possible with mod_rewrite?
EDIT:
I added
127.0.0.1 localhost
127.0.0.1 wiki2
127.0.0.1 wiki3
to the hosts file and
VirtualHost 127.0.0.1>
ServerName wiki2
ServerAlias 127.0.0.1
DocumentRoot c:/xampp/htdocs/wiki_angua
<Directory c:/xampp/htdocs/wiki_angua >
Allow From All
</Directory>
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName wiki3
ServerAlias 127.0.0.1
DocumentRoot c:/xampp/htdocs/weatherwax
<Directory c:/xampp/htdocs/weatherwax >
Allow From All
</Directory>
</VirtualHost>
to httpd.conf and restarted apache.
Whether I open wiki2 or wiki3 I land in ./wiki_angua.
Is there anything I forgot?
If you have a DNS entry for server2 then the Host HTTP request header will be correctly set, and all you need then is a virtual host, without the need to use mod_rewrite.
For example:
<VirtualHost *:80>
ServerName server2
DocumentRoot /path/to/server1/sub
<Directory /path/to/server1/sub>
Allow From All
</Directory>
# ... etc
</VirtualHost>
edit:
In the case you still want to use mod_rewrite you can do something like:
RewriteCond %{HTTP_HOST} ^server2$
RewriteRule ^(.+) /path/to/server1/sub/$1
This must be located in the global server configuration, and not in an existing virtual host.