Redirection of domain within <VirtualHost> tag - regex

I have the following in my apache2.conf file for a django project:
#ServerRoot "/etc/apache2"
<VirtualHost *:80>
Alias /static /home/ubuntu/Avails/static
# virtual host configuration
WSGIScriptAlias / home/ubuntu/Avails/wsgi.py
</VirtualHost>
Similar to a previous question I asked here (How to redirect one domain to another).
I have two domains:
www.new.example1.com/ --> IP=1.2.3.4
www.new.example2.com/ --> IP=1.2.3.4 (same IP for both domains)
I want all requests that go to www.example2.com/* to access the requested page. I want all requests that go to www.example1.com/* to redirect to www.example2.com/*. how would I redirect a domain within the VirtualHost block, instead of using the .htaccess file?

Here is what worked. Special thanks to anubhava for the help:
1) Enable module rewrite:
$ sudo a2enmod rewrite
2) Edit apache2.conf file:
<VirtualHost *:80>
Alias /static /home/ubuntu/Avails/static
# virtual host configuration
WSGIScriptAlias / home/ubuntu/Avails/wsgi.py
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new\.example1\.net [NC]
RewriteRule ^ http://new.example2.net%{REQUEST_URI} [NE,R=301,L]
</VirtualHost>
3) Restart apache:
$ sudo service apache2 restart

Related

Amazon Linux httpd redirecting to www not working

I am actually trying to do 2 things:
1. convert site port from 80 to 3000
2. redirect site from example.com to www.example.com
My machine is amazon linux, so using http
so in httpd.conf file, below is what I've added:
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ProxyPreserveHost On
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "http://www.%{HTTP_HOST}/$1" [L,R,NE]
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
In above, port changing is working, but redirecting to www not working, could you please suggest me what changes I need to make, to make it to work in above.

Redirect from HTTP to HTTPS in Python Django

I want to redirect the python and django websites from http to https.
When I'm using the SECURE_SSL_REDIRECT = True in settings.py,
I'm getting the folder structure:
Are you using apache?
Try this on your .htacess inside www folder.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
supposing that:
you are using apache2;
you are starting from the default-ssl.conf file which, if you are using a debian-like distro, is located in the directory /etc/apache2/sites-available;
you are serving your django application using mod_wsgi, having modified your default-ssl.conf as described in this howto
you could try defining a permanent redirect to https in the <VirtualHost *:80> directive, always in your default-ssl.conf, as shown below:
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName your-server.name
Redirect permanent / https://your-server.name
</VirtualHost>
<VirtualHost _default_:443>
ServerName your-server.name
...

Apache2 : Limit virtual host to one domain name

i want to do something but i can't find an answer (Maybe i baldy searched). I don't know if it is possible so say me if it's not.
I would like to limit an apache virtual host to one and only domain name : bde.yggdrasil.cafe. So that if the user try to access this website using 90.90.3.57 or another domain name it is listed as not existing website. Here is my extra/bde.conf which is included in httpd.conf, you'll understand the problem :
<VirtualHost *:80>
ServerName bde.yggdrasil.cafe
ServerAdmin my#email.fr
DocumentRoot /srv/http/bdeweb
#Some django config
#[...]
RewriteEngine on
RewriteCond %{SERVER_NAME} =bde.yggdrasil.cafe
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Listen 443
<VirtualHost *:443>
ServerName bde.yggdrasil.cafe
ServerAdmin my#email.fr
DocumentRoot /srv/http/bdeweb
#Some django config
#[...]
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/bde.yggdrasil.cafe/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/bde.yggdrasil.cafe/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
If i use this code and i try to connect to 90.90.3.57 or yggdrasil.cafe using http protocol my request is catched by django which return an error 400 (It is good but not what i want to get). if i connect to bde.yggdrasil.cafe using http it redirect me correctly to https.
DNS Redirection
I think it can be usefull so i give you my DNS Redirections :
yggdrasil.cafe --> 90.90.3.57
bde.yggdrasil.cafe --> yggdrasil.cafe
Removing Django config
If i remove the django configuration in my HTTP virtual host and i use the following file :
<VirtualHost *:80>
ServerName bde.yggdrasil.cafe
RewriteEngine on
RewriteCond %{SERVER_NAME} =bde.yggdrasil.cafe
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Listen 443
<VirtualHost *:443>
ServerName bde.yggdrasil.cafe
ServerAdmin my#email.fr
DocumentRoot /srv/http/bdeweb
#Some django config
#[...]
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/bde.yggdrasil.cafe/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/bde.yggdrasil.cafe/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
If i try to connect to my server using 90.90.3.57 or yggdrasil.cafe i fall on "Indexes of /" (Wich is bad, realy bad).
I would like apache to ignore the connection if the user doesn't user the subdomain "bde.yggdrasil.cafe".
Why i want to do that ?
This domain is for all my stuff, including a future personal showcase website i would like to host on "yggdrasil.cafe" domain name (without subdomain).
So the important is not the result but i would like to know if it is possible to add an other VirtualHost on "yggdrasil.cafe" domain name afterward without django catching it.
Thanks for your time :)
Solution
As said by Dusan Basic
Of course, just add another VirtualHost with ServerName yggdrasil.cafe; for the beginning you can have simple RewriteRule ^ - [F]
It fixed the problem :)

Apache config with flask, wsgi, and reverse proxy

Below is my Apache config. What I am trying to do have a reverse proxy set up for the main part of the site. So when users go to mysite.xyz, they get what is running on port 5000 (this is working). I also want to run a flask app on mysite.xyz/page1 This is coming back with a 404 not found page. When I switch them and put the app at / and put the main site at page1, then the app works but page1 can't find any of the correct js assets so I'd like to avoid that. But the app does work and works on apache but the reverse proxy must be over-riding the WSGI deployment of the flask app or something?
<VirtualHost *:80>
ServerName mysite.xyz
ServerAdmin email#gmail.com
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
RewriteCond %{HTTP_HOST} !^mysite\.xyz$ [NC]
RewriteRule ^/$ http://%{HTTP_HOST}/ [L,R=301]
WSGIScriptAlias /page1 /var/www/FlaskApp/FlaskApp.wsgi
<Directory /var/www/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Instead of ProxyPass, try:
RewriteRule ^/$ http://127.0.0.1:5000/ [P]
ProxyPassReverse / http://127.0.0.1:5000/
I know this is stale but it may help someone. Flask hates reverse proxy. You need to add flask-reverse-proxy-fix to your app.
https://pypi.org/project/flask-reverse-proxy-fix/

Configure apache to respond with error page for incorrect hosts

django docs:
You should also configure the Web server that sits in front of Django to validate the host. It should respond with a static error page or ignore requests for incorrect hosts instead of forwarding the request to Django. This way you’ll avoid spurious errors in your Django logs (or emails if you have error reporting configured that way). For example, on nginx you might setup a default server to return “444 No Response” on an unrecognized host:
I am using Apache and this works:
http://serverip -> 404 error
http://www.example.com -> https://www.example.com -> django site
http://example.com -> https://example.com -> django site
Now I have a problem with https://serverip
I get a not secure message by my browser because I only have a SSL Cert for example.com, www.example.com and after I accept the security warning I see the django site but I want 404 error page.
How can I achieve this or do I misunderstand the django docs?
Update Config:
assume the django site is /var/www/html/index.html for simplification
000-default.conf:
<VirtualHost *:80>
Redirect 404 /
</VirtualHost>
example.com.conf:
<VirtualHost *:80>
ServerAdmin example#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
example.com-le-ssl.conf:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin example#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>