Inboud rules for https is not accepting in aws ec2 - amazon-ec2

I'm not able to make Inbound rules of https in aws ec2, because I have to make force http to https redirection. http://example.com to https://example.com.
Here is my Virtual Host file /etc/apache2/sites-available/000-default.conf
DocumentRoot /var/www/example/public/
RewriteEngine On
RewriteCond %{HTTP-X-Forwarded-Proto} =http
RewriteRule .* https://example.io [L,R=permanent]

try with following steps
Allow 80 and 443 port in inbound rule of concernd security group.
Add following code in httpd.conf file then remove old code you have written for the redirection
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

How can I block apache2 root path to a proxy? is it even possible?

So I am hosting an icecast server and I want to proxy ssl termination to icecast using apache2.
I was using an nginx proxy to icecast which was perfect but there were some complications with nginx and php, so i have to use apache2.
so I am trying to achieve the same proxy as what I achieved in nginx but I simply cannot block the root path without blocking everything or it simply not working.
here is my virtualhost below, I have added some comments for some attempts (2 of about 100 attempts lol)
<VirtualHost *:8050>
ServerAlias *.mydomain.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/mydomain.com.pem
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/stats.xsl [NC]
RewriteCond %{REQUEST_URI} ^/index.html [NC]
# this doesnt work. I expect because of the Rewrite Rule below?
#RewriteCond %{REQUEST_URI} ^/ [NC]
RewriteRule .* - [F,L]
# this doesnt work it blocks everything
#<Location />
# Order Allow,Deny
# Deny from all
#</Location>
ErrorLog ${APACHE_LOG_DIR}/mediaserverproxy-error.log
CustomLog ${APACHE_LOG_DIR}/mediaserverproxy-access.log combined
ProxyPass / http://localhost:80/
ProxyPassReverse / http://localhost:80/
</VirtualHost>
Just to be clear I know you can do ssl in icecast but it reduces stability when sending icecast HUP signals for reloads.
Any help is massively apreciated
What is the expected outcome?
That a request for / will return a 404 and any other request will serve that path with a 200?

How can I use mod_rewrite to redirect multiple hosts between domains?

I'm trying to redirect requests for mis-spelt domain names to the same server on the official domain.
My apache configuration looks like:
<VirtualHost *:80>
RewriteEngine on
# Fix domain spellings in host.<backupdomain>
RewriteCond %{HTTP_HOST} !([^.]+).example.com [NC]
RewriteRule ^/(.*) http://%1.example.com/$1 [NC,R,L]
</VirtualHost>
I know I'm close, because the requests to server99.wrongdomain get re-written to .example.com - and I'm expecting it to go to server99.example.com.
Why isn't the regex capture/expansion working correctly here?
P.S. Incredibly annoying that SO is blocking my original examples because they look like links (!)
If you want to match something not followed by something else then you can use Negative lookahead.
RewriteCond %{HTTP_HOST} ^([^.]+)\.(?!example\.com) [NC]
RewriteRule ^/(.*)$ http://%1.example.com/$1 [NC,R,L]
This way, each wrong domain (with server99 for example)
server99.example.co.uk
server99.exampel.com
etc
will redirect to server99.example.com.
That is virtually everything you need in case you only want to redirect misspelled subdomains.
<VirtualHost *:80>
ServerName *.example.com
RedirectMatch 301 /(.*) http://www.example.com/$1
</VirtualHost>
If you want to redirect every request for which there is no uniqe VHost configuration, use the following and make sure it is the very first VHOST configuration loaded by apache
<VirtualHost _default_:80>
RedirectMatch 301 /(.*) http://www.example.com/$1
</VirtualHost>
Of course that only works if the DNS record of the FQDN points to the apache in question.

mod_rewrite getting rid of port number in favor of directory

How do I mod_rewrite ip:4040 to ip/streamer ?
I have application at ip:4040 but it is hard for users to remember port number.
So I created directory /var/www/streamer
And now I'm creating apache virtualhost:
<VirtualHost *:80>
DocumentRoot /var/www/streamer
RewriteEngine On
RewriteRule / :4040
</VirtualHost>
Obviously rewriteRule is incorrect, but having no idea of regex, any thoughts on how to do this?
Assuming your web server is also listening on port 80, as well as 4040:
RewriteEngine On
RewriteCond %{HTTP_HOST} 4040
RewriteRule .* http://xxxxxxx/ [L,R=301]
Where xxxxxxx is the server host name or IP, without a port number.

How to configure mod_proxy to block every site except one

I'm trying to set up mod proxy to block all traffic except to a specific domain. I can configure it to block individual domains using the ProxyBlock directive, and I can block everything using ProxyBlock *. Is there a way to block everything but one domain?
Thanks,
-Andrew
On apache 2.2 you need to have 2 proxy sections.
ProxyRequests On
ProxyVia On
# block all domains except our target
<ProxyMatch ^((?!www\.proxytarget\.com).)*$>
Order deny,allow
Deny from all
</ProxyMatch>
# here goes your usual proxy configuration...
<ProxyMatch www\.proxytarget\.com >
Order deny,allow
Deny from all
Allow from 127.0.0.1
</ProxyMatch>
On apache 2.4 it would be much easier because you could use the If directive instead of that regexp to invert the match for the domain name.
Note: I got that regexp from Invert match with regexp
Try:
ProxyBlock *
ProxyPass <path> <destination>
See if that works.
EDIT: scratch that. I think you have to get creative here with mod_rewrite (the basic reference is at http://httpd.apache.org/docs/current/rewrite/proxy.html):
RewriteCond %{HTTP_HOST} =allowtoproxy.com
RewriteRule ^/(.*)$ http://proxytarget.com/$1 [P]
ProxyPassReverse / http://proxytarget.com/
Try that?
Try this code:
RewriteEngine On
# Testing URLs
RewriteCond %{HTTP_HOST} !google.co.uk [NC]
RewriteCond %{HTTP_HOST} !bbc.co.uk [NC]
RewriteCond %{HTTP_HOST} !amazon.com [NC]
RewriteCond %{HTTP_HOST} !centos.org [NC]
RewriteCond %{HTTP_HOST} !opensuse.org [NC]
# Url to redirect to if not in allowed list
RewriteRule (.*) http://example.org/notallowed.htm

Apache Virtual Hosts and mod_rewrite conflicts

I've been scouring the net and SO and I can't get around or through this problem.
We have a bunch of subdomains and a few dedicated servers. One server does double-triple duty as issue tracking and landing page. Problem is the wildcard landing page doesn't take you to the correct virtual host page sometimes. I've been looking at wildcards but they seem particularly broad.
Our scenario is the following:
-www.askia.com is the main landing site. A non-existing (wildcard) subdomain should always land here.
-dev.askia.com is the technical support and issues site. It has some mod_rewrites for https. It took me a while, but I got it to work and I'd rather not break it.
-www.askia.fr is our french site. Instead of taking you to www.askia.com it takes you to the dev.askia.com.
-www.askia.co.uk should take you to www.askia.com but it goes to dev.askia.com
I'm not entirely sure where I should be trying to fix the solution. Should I do something in the CNAME. In the virtualhosts config file or in the mod_rewrite file.
Try these rules:
RewriteCond %{HTTP_HOST} ^dev\.
RewriteCond %{HTTP_HOST} !^dev\.askia\.com$
RewriteRule ^ http://dev.askia.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.askia\.com$
RewriteCond %{HTTP_HOST} !^dev\.askia\.com$
RewriteRule ^ http://www.askia.com%{REQUEST_URI} [L,R=301]
The first rule redirects every request to a host starting with dev. but not dev.askia.com to www.askia.com. And the second rule redirect requests to a host other than www.askia.com and dev.askia.com to www.askia.com. So every request should either go to dev.askia.com or www.askia.com.
When using Virtual Hosts in Apache the first hosted listed will always be the default for non-matches.
#default vhost
# any non-matches will land here
<VirtualHost _default_:80>
ServerName www.askia.com:80
DocumentRoot /path/to/site
ErrorLog /path/ti/sites/logs/error_log
</VirtualHost>
# vhost #2
<VirtualHost _dev_Site_:443>
ServerName dev.askia.com:443
DocumentRoot /path/to/dev/site
ErrorLog /path/to/dev/sites/logs/error_log
#ssl details
SSLEngine on
SSLCipherSuite HIGH:MEDIUM
SSLCertificateFile /location/securti.crt
SSLCertificateKeyFile /location/securti.key
#any rewrite rules to apply only to this (default) domain
# force SSL for instance..
RewriteRule .* - [F]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://dev.askia.com/
</VirtualHost>
#etc, etc

Resources