htaccess https for several subdomains - apache

I have virtual hosts
<VirtualHost 10.10.10.10:80>
ServerAdmin webmaster#server.com
ServerName server.com
ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>
<VirtualHost 10.10.10.10:443>
ServerAdmin webmaster#server.com
ServerName server.com
ServerAlias subdomain-a.server.com subdomain-b.server.com subdomain-c.server.com subdomain-d.server.com
DocumentRoot /srv/www/server.com/public_html/
</VirtualHost>
I want to force visitors use https for subdomain-a and subdomain-c. Visitors of subdomain-b and subdomain-d could use http and https. How to configure .htaccess?

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(subdomain-a|subdomain-c)\.server\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

Virtual Host HTTPS redirection in AWS

Am having two domains abc.example.com and xyz.example.com. I got SSL certificate for *.example.com from AWS (where i can't download SSL certificate).Am using Virtual host file for pointing two domains to two different folder /var/www/html/abc and /var/www/html/xyz. Now I want to use Redirect from http to https for both the domains in virtual host.I tried virtual host code something like this:
< VirtualHost *:80 >
ServerAdmin root#localhost
DocumentRoot "/var/www/html/abc"
ServerName abc.example.com
Redirect permanent / https://abc.example.com/
ServerAlias www.abc.example.com
< /VirtualHost >
< VirtualHost *:80 >
ServerAdmin root#localhost
DocumentRoot "/var/www/html/xyz"
ServerName xyz.example.com
Redirect permanent / https://xyz.example.com/
ServerAlias www.xyz.example.com
< /VirtualHost >
And also tried *:443 instead of *:80. But I couldn't get any results.
Please Help me to solve this. Thanks in advance.
After spending lots of hour , finally found a solution for the problem.
The code works in this way:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin root#localhost
VirtualDocumentRoot "/var/www/html/abc"
ServerName abc.example.com
<Directory "/var/www/html/abc">
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin root#localhost
VirtualDocumentRoot "/var/www/html/xyz"
ServerName xyz.example.com
<Directory "/var/www/html/xyz">
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
CloudFront can redirect to HTTP to HTTPS for you automatically:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-https-viewers-to-cloudfront.html

Apache Virtual Host with Angular Fullstack (NodeJS) App 307 Redirect

I have an Ubuntu server with multiple php applications running with Apache on port 9999.
At the same time, my NodeJS app on the same server runs on port 8080 and I use apache's virtual host to redirect requests from port 80 or 443 to my NodeJS app.
My virtual host looks like that:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /ssl/example.com.pem
SSLCertificateKeyFile /ssl/example.com.key
SSLCACertificateFile /ssl/intermediate.pem
ServerAdmin webmaster#localhost
DocumentRoot /home/marcel/www/example
Options -Indexes
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyRequests on
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
Now all requests to example.com should be redirected to https://www.example.com. I have this .htaccess file.
Options -MultiViews
ErrorDocument 404 /404.html
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
# RewriteBase /
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
My Problem: When I go to http://example.com, I get redirected to https://example.com (without www, but ok) and I get a 307 redirect! I want 301 for SEO reasons. Same thing happens if I do www.example.com => it goes to https://www.example.com but also with 307 redirect.

Apache VirtualHost: strip www. and force https

I am working on a site that only owns an SSL cert for monsite.fr. I am trying to remove the www. and redirect to https://monsite.fr.
the redirection doesn't work if the user type www.monsite.fr, he WILL NOT be redirected to https://monsite.fr but to https://www.monsite.fr and get certification error NET::ERR_CERT_COMMON_NAME_INVALID.
This is the content of mysite.conf file:
<IfVersion < 2.3 >
NameVirtualHost *:80
NameVirtualHost *:443
</IfVersion>
<VirtualHost *:80>
ServerName monsite.fr
Redirect / https://monsite.fr/
#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
</VirtualHost>
<VirtualHost *:80>
ServerName www.monsite.fr
Redirect / https://monsite.fr/
</VirtualHost>
<VirtualHost *:443>
ServerName monsite.fr
ServerAlias www.monsite.fr
#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ https://%1$1 [R=permanent,L]
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://monsite.fr/$1 [R,QSA,L]
DocumentRoot "/opt/monsite.fr/htdocs"
</VirtualHost>
Is there a solution to redirect www.monsite.fr to https://monsite.fr ?
Modify your vhost configuration as follows:
<VirtualHost *:80>
ServerName www.monsite.fr
ServerAlias monsite.fr
RedirectMatch ^/(.*)$ https://monsite.fr/$1
</VirtualHost>
<VirtualHost *:443>
ServerName www.monsite.fr
RedirectMatch ^/(.*)$ https://monsite.fr/$1
</VirtualHost>
<VirtualHost *:443>
ServerName monsite.fr
SSLEngine On
SSLCertificateFile /path_to_cert/server.crt
SSLCertificateKeyFile /path_to_key/server.key
DocumentRoot "/opt/monsite.fr/htdocs"
</VirtualHost>
The first virtual host block will redirect all requests from http://www.monsite.fr to https://monsite.fr. It will also redirect all requests from http://monsite.fr to https://monsite.fr
The second virtual host block will redirect all requests from https://www.monsite.fr to https://monsite.fr.
The third virtual host block will serve the content for https://monsite.fr. Make sure to edit the above mentioned configuration and add the correct path /path_to_cert/server.crt for the SSL certificate and /path_to_key/server.key for the private key.

mod_rewrite `Redirect` how?

I have 2 urls say thinkingmonkey.me and thinkingmonkey.com both have ip-address 127.0.0.1 (A.K.A localhost).
I want to redirect any requests to thinkingmonkey.com to thinkingmonkey.me.
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot /mysite
ServerName thinkingmonkey.me
ServerAlias www.thinkingmonkey.me
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
Options -Indexes +FollowSymLinks
RewriteEngine On
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName thinkingmonkey.com
Redirect thinkingmonkey.com http://thinkingmonkey.me/
# Redirect / http://thinkingmonkey.me/ #have even tried this
ServerAlias www.thinkingmonkey.com
RewriteEngine on
</VirtualHost>
When I try to access thinkingmonkey.com the url does not get redirected to thinkingmonkey.me. The url in the brower's address bar remains thinkingmonkey.com.
What am I doing wrong?
mod_rewrite is considered better and more powerful to handle these rewrites. You can use following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?thinkingmonkey\.com$ [NC]
RewriteRule ^ http://%1thinkingmonkey.me%{REQUEST_URI} [L,R=301]
If you really-really want to use mod_aias you can put this line in VirtualHost section of thinkingmonkey.com:
Redirect 301 / http://thinkingmonkey.me/
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !.*thinkingmonkey\.me$
RewriteRule ^/(.*)$ http://thinkingmonkey.me/$1 [R]
The documentation: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

.htaccess rewrite rules for www.example.com and example.com -> this.example.com

I would like my site to direct all traffic from www.example.com and example.com or anythingelse.example.com to this.example.com This is what I have in my .htaccess file which is located in /www/example.com/public_html directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://this.example.com/ [R=301,L]
It works when I visit this.example.com but when I visit http://example.com it redirects me to example.com/public_html and www.example.com doesn't redirect at all! However this.example.com works. This is my virtual host setting:
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias this.example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
</VirtualHost>
I've noticed in my error log when I visit: example.com it says "File does not exist: /srv/www/example.com/public_html/public_html"
Solution provided by requinix on devshed
RewriteEngine On
RewriteCond %{HTTP_HOST} !this.example.com
RewriteRule ^ http://this.example.com%{REQUEST_URI} [L]