One DocumentRoot, Multiple Domains, Adding www but no htaccess? - apache

I have multiple domains pointing to the same folder, but I'd like some to be www and some non-www. Right now something is causing all of them to go straight to the www version and I am not sure why.
I have one VirtualHost setup with ServerAlias for all the domains as such:
<VirtualHost *:80>
ServerAdmin info#domain.com
DocumentRoot "c:/Apache24/htdocs/domain1"
ServerName domain1.com
ServerAlias www.domain1.com
ServerAlias domain2.com
ServerAlias www.domain2.com
ServerAlias domain3.com
ServerAlias www.domain3.com
ServerAlias domain4.com
ServerAlias www.domain4.com
ErrorLog "c:/logs/www/domain1-error.log"
#CustomLog "c:/logs/www/domain1-access.log" common
CustomLog "| c:/Apache24/bin/rotatelogs.exe C:/logs/www/domain1/access-%Y_%m_%d.log 86400 -300" combined
# php version #
AddHandler fcgid-script .php
FcgidWrapper "c:/php/php-cgi.exe" .php
</VirtualHost>
I'd like to figure out why http://domain3.com/info.php goes to http://www.domain3.com/info.php and so on.
How can I get it to stay at http://domain3.com/info.php and even get http://www.domain3.com/info.php to switch to http://domain3.com/info.php
I'd like to figure out if this in an Apache setting as I understand I can use htaccess to change around www or not, but right now I don't have any htaccess in the DocumentRoot folder and it's still redirecting to www for everything.
Thanks!

Related

Redirect sub domain A to https domain B

I've tried looking for a post related to my issue but I couldn't find a suitable one. If there is, let me know!
Here's the current situation that I'm facing now. I would like to redirect a domain,
example.hr
that is being used with another server and there's an SSL that comes with it.
Now, I have another server that comes with the domain,
example.co
and there's an SSL cert too.
I would like to redirect test.example.hr to https://example.co. How can I go about to do this? I'm testing it with the subdomain since the root domain is in used.
I've tried this method,
<VirtualHost *:80>
ServerName test.example.hr
ServerAlias www.test.example.hr
Redirect / https://example.co/
</VirtualHost>
<VirtualHost *:443>
ServerName example.co
ServerAlias www.example.co
DocumentRoot /var/www/html
ErrorLog /var/www/html/error.log
CustomLog /var/www/html/access.log combined
</VirtualHost>
The SSL configuration is inside the 443 block.
When I go to test.example.hr, it will change to https://test.example.hr and the error that comes up is "Your connection is not private. Attackers might be trying to steal your information from test.alt.hr (for example, passwords, messages or credit cards). Learn more
NET::ERR_CERT_COMMON_NAME_INVALID"
Can you try this and update me.
<VirtualHost *:80>
ServerName test.example.hr
ServerAlias www.test.example.hr
Redirect / http://example.co/
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
ServerName example.co
ServerAlias www.example.co
DocumentRoot /var/www/html
ErrorLog /var/www/html/error.log
CustomLog /var/www/html/access.log combined
</VirtualHost>
make sure to enable a2enmod rewrite

apache2 VirtualHost subdomain configuration not working

All i want is, that www.example.com redirects to example.com, that works, but i can't add another VirtualHost which would allow admin.example.com for admins.
Here are my .conf file:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias old.website.com
DocumentRoot /var/www/redirect
<VirtualHost *:80>
ServerName admin.example.com
DocumentRoot /var/www/admin
and
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
However, if i enter admin.example.com i get redirected to example.com and if i use anything else as subdomain, for example test.example.com or anything.example.com the normal example.com site is shown BUT in the URL field you can still see the original URL (anything.example.com, ...) which isn't the case when using admin.example.com. My .htaccess file in the /var/www/redirect directory looks like that:
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
ErrorDocument 404 /index.html
Ok it seemed to be a google-chrome only error that was caused by my browser data, if anybody else is having problems with it, try using Icognito Mode in Chrome, it should work there.
Thanks to Chand Prakash for helping me

Virtual hosts and www redirects causing infinate redirects

Hi I have two virtual hosts and I am trying to redirect non www to www without using the RewriteEngine, it works just fine if I only add the redirect to one of them, but when I add it to both the sites it causes an infinite redirect.
Please I want to do this without RewriteEngine.
Also I'm doing this in the Apache config file.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html/site1
ServerName site1.com
ServerAlias site1.com
Redirect 301 / http://www.site1.com/
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/site2
ServerName site2.com
ServerAlias site2.com
Redirect 301 / http://www.site2.com
</VirtualHost>
I think you're missing virtualhost that catches www subdomain that may cause infinite redirect. Try this:
//Redirect non-www to www
<VirtualHost *:80>
ServerName site1.com
Redirect permanent / http://www.site1.com/
</VirtualHost>
and then set up another <VirtualHost> section with ServerName www.site1.com so it catches www one.

Wildcard sub.domain.tld redirect to domain.tld/sub

I currently have a wild card subdomain with this on apache:
<VirtualHost xxx.xxx.xxx.xxx:80>
DocumentRoot /home/web/domaine.tld/www
ServerName domaine.tld
ServerAlias *.domaine.tld
VirtualDocumentRoot /home/web/domaine.tld/%1
CustomLog /var/log/apache2/domaine.tld/access.log combined
ErrorLog /var/log/apache2/domaine.tld/error.log
</VirtualHost>
And need to have any subdomain like sub.domain.tld being redirected to domain.tld
You are able to use Redirect via .htaccess
Redirect http://sub.domain.tld http://domain.tld

htaccess Domain Simulation

I have a server that hosts stuff automatically from /var/www. I copied a directory like domain.com inside /var/www. I then added domain.com into my /etc/hosts for 127.0.0.1 (localhost/loopback). What's the .htaccess trick with Apache so that I can hit my site with: http://domain.com/ and it automatically knows to look in /var/www/domain.com/ (without redirection of the URL)?
You need a virtual host like this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/domain.com/
ServerName domain.com
</VirtualHost>
More information on the Apache documentation: http://httpd.apache.org/docs/2.0/vhosts/examples.html
I think you are looking for:
<VirtualHost *:80>
ServerAdmin mail#domain.com
DocumentRoot /var/www/htdocs/domain.com/
ServerName *.domain.com
ErrorLog logs/domain-error_log
CustomLog logs/domain-access_log common
</VirtualHost>
this will look for anything comming in on *:80 and if it's domain.com its DocRoot becomes /var/www/htdocs/domain.com