Virtual host not working AWS apache - apache

I have following virtual host ( in apache2.conf file ) to load all subdomain from a single directory on AWS
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName mydevsite.com
ServerAlias mydevsite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/apps
ServerName mydevsite.com
ServerAlias *.mydevsite.com
</VirtualHost>
It not working and subdomains are still pointing to html directory
I tried following in htaccess as well but then it gives me 500 error
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
My simple requirement is to load main domain from root directory and all subdomains from apps directory

Check the Doc: Using Name-based Virtual Hosts, ServerName Directive, ServerAlias Directive
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.mydevsite.com
ServerAlias mydevsite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/apps
ServerName apps.mydevsite.com
ServerAlias *.mydevsite.com
</VirtualHost>

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 HTTP to HTTPS with VirtualHost

I have Let's Encrypt SSL certificate for exampledomain.com. www.exampledomain.com redirects to https://exampledomain.com, but exampledomain.com gives "Apache2 Ubuntu Default Page". I use following .conf file for domain in sites-available folder:
<VirtualHost *:80>
DocumentRoot /var/www/html/mydomain
ServerName mydomain.hu
ServerAlias www.mydomain.hu
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.hu [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.hu
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Redirect / https://mydomain.hu/
</VirtualHost>
EDIT:
Modified .conf file with use of answers.
<VirtualHost *:80>
DocumentRoot /var/www/html/mydomainamehu
ServerName mydomainame.hu
ServerAlias www.mydomainame.hu
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomainame.hu [OR]
RewriteCond %{SERVER_NAME} =www.mydomainame.hu
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Redirect / https://mydomainame.hu/
</VirtualHost>
<VirtualHost *:80>
ServerName mydomainame.hu
ServerAlias www.mydomainame.hu
RewriteEngine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
ServerName mydomainame.hu
Redirect 301 / https://mydomainame.hu/
</VirtualHost>
<VirtualHost *:443>
ServerName mydomainame.hu
DocumentRoot /var/www/html/mydomainamehu
</VirtualHost>
VirtualHost must listen on port 443 when using https.
I usually listen on port 80, add a redirect there and then config the docroot in a separate VirtualHost:
#this listens on port 80 and redirects to https
<VirtualHost *:80>
ServerName mydomain.hu
ServerAlias www.mydomain.hu
Redirect 301 / https://mydomain.hu
</VirtualHost>
#this is to avoid "duplicate" traffic on www
<VirtualHost *:443>
ServerName www.mydomain.hu
Redirect 301 / https://mydomain.hu/
</VirtualHost>
#here is your docroot so you don't get "Apache2 Ubuntu Default Page"
<VirtualHost *:443>
ServerName mydomain.hu
DocumentRoot /home/www/whatever
</VirtualHost>
Please note that you should also add access rules and certificates configuration (if needed), otherwise this is not going to work.
You may use a redirect rule like that:
<VirtualHost *:80>
ServerName mydomain.hu
ServerAlias www.mydomain.hu
RewriteEngine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
</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>

Redirect subdomains to different paths while using separate SSL certificates

I have a website with several different subdomains, and want to have parts of the site with higher levels of security than others. Such as Admin areas. I know that you can create as many certificates as you want. The issue that I am having is that whatever subdomain is listed first, it's DocumentRoot is applied to all the other subdomains redirecting to https://. Here is my code in httpd-vhosts.conf:
# HTTP Configuration
<VirtualHost *:80>
ServerName account.example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
# SSL Configuration
<VirtualHost *:443>
ServerName account.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/account.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/account.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
#Secure Admin Config
<VirtualHost *:80>
ServerName secure.example.com
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
# Other directives here
</VirtualHost>
#SSL Config for Admin area
# SSL Configuration
<VirtualHost *:443>
ServerName secure.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/secure.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/secure.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
</VirtualHost>
For example, if I were to go to secure.example.com it's DocumentRoot would be /Applications/MAMP/htdocs/Website/Account instead of /Applications/MAMP/htdocs/Website/Secure
Is there a way to fix this?
Thank you in advance!

Apache: virtualhost on each sub-domain to corresponding directory

I would like to do something like this:
<VirtualHost *:80>
ServerName $variable.example.com
DocumentRoot /var/www/$variable
</VirtualHost>
for example if I go to foo.example.com it must show me /var/www/foo directory content
What is the good apache syntax for that?
You can't have $variable like that in servername and map it to the document root, but you can use mod_rewrite and a wildcard.
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAlias *.example.com
DocumentRoot /var/www/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*::\1
RewriteRule ^(.*)$ /%1/$1 [L]
</VirtualHost>
The subdomain.example.com can just be any subdomain that isn't www.example.com. For any request that doesn't start with "www", the subdomain is captured as a group (2nd condition), then the next line makes sure the request isn't already being routed into the subdomain's name, and the rule routes it.
So for the request:
http://foo.example.com/bar/x.html
it gets routed to:
/foo/bar/x.html
This is what I use with my freelancer work:
Under httpd-vhosts.conf:
UseCanonicalName Off
<VirtualHost *:${AP_PORT}>
DocumentRoot ${US_ROOTF_WWW}/_client
ServerName client
ServerAlias client
<Directory "${HOME}\www\_client">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:${AP_PORT}>
VirtualDocumentRoot "${US_ROOTF_WWW}/_client/%1"
ServerName subdomains.client
ServerAlias *.client
<Directory "${HOME}\www\_client\*">
Require all granted
</Directory>
</VirtualHost>
Then under the windows hosts file place the domain and subdomains that are needed.
127.0.0.1 client
127.0.0.1 someone.client
127.0.0.1 someone2.client