apache2 VirtualHost subdomain configuration not working - apache

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

Related

htaccess for url redirection with virtualhosts

I have an Apache server running 3 virtual hosts:
<VirtualHost *:80>
DocumentRoot "/var/www/sub1"
ServerName sub1.domain.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/sub2"
ServerName sub2.domain.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/sub3"
ServerName sub3.domain.com
# Other directives here
</VirtualHost>
I would like to set up URL redirection on sub3 so that when sub3.domain.com/go/7dj29 is requested, it redirects to sub3.domain.com/go/redirect.php?id=7dj29.
I've created an .htaccess in /var/www/sub3:
RewriteEngine On
RewriteRule ^\/go\/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1 [QSA,L]
However, I receive a 404 instead of a redirect; what am I doing wrong?
Not Found
The requested URL /go/7uja8 was not found on this server.
The following rule works.
RewriteRule ^go/([a-zA-Z0-9]{5,})$ /go/redirect.php?id=$1
For apache to load rules from .htaccess you need at least AllowOverride FileInfo, but you can set it directly in VirtualHost context without it.

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

About Apache Rewrite Function to Shorten URL

I have an URL "http://10.21.50.66:8123/test/file/index.htm" which hosted in Apache server. I would like to shorten the URL to just "http://10.21.50.66:8123/" and I managed to do it by using Rewrite function in httpd.conf. However, when I type "http://10.21.50.66:8123/" in browser, it will redirect and the URL of the browser will change back to the long url "http://10.21.50.66:8123/test/file/index.htm". What I would like to archive is that when every time I type "http://10.21.50.66:8123" in browser, the browser will open "http://10.21.50.66:8123/test/file/index.htm" but the URL on the browser will still showing "http://10.21.50.66:8123/". Below is the currecnt setting:
RewriteEngine On
RewriteRule ^/$ http://10.21.50.66:8123/test/file/index.htm [R,L]
Any ideas?
Thanks in advance.
If you use [R], the Apache will return response code 302 to the browser (with the new Location), which will cause that new URL to appear in address bar.
try:
RewriteEngine On
RewriteRule ^/$ /test/file/index.htm [L]
You need to create a virtual host in httpd.conf. You might find it in extras folder too as httpd.vhosts.conf Its something like this:
<VirtualHost *:80>
ServerAdmin admin#test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
you need to write something like:
<VirtualHost 10.21.50.66:8123>
ServerAdmin admin#test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot [path to your test/file folder]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

HTTPS by default for a single website on Apache

I have a WAMP server with a few sites on it. I would like to enable SSL for default for only one of the sites.
While the https://www.example.com is accessible, there is no auto redirect happening for http://www.example.com.
Also httpd -t shows Syntax Ok
Please help.
This is my httpd-vhosts.conf file
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin hi#santo.sh
DocumentRoot "C:/wamp/www/example"
ServerName http://manage.example.com/
ServerAlias http://manage.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin hi#santo.sh
DocumentRoot "c:/wamp/www/example/public"
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile "C:/wamp/OpenSSL/cert/sslcert.cert"
SSLCertificateKeyFile "C:/wamp/OpenSSL/certs/mydomain.key"
</VirtualHost>
This questions was answered well on ServerFault
This part is wrong.
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
While there are many choices available: here's the best one:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
# [ Http to Https ]
Redirect 301 / https://www.example.com/
</VirtualHost>

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

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!