Redirect ip address to https server name - apache

So this is my conf:
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName ./install.sh
</VirtualHost>
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName test.com
ServerAlias www.test.com
Redirect permanent / https://test.com/
RewriteEngine on
#RewriteBase /
RewriteCond %{SERVER_NAME} =www.test.com [OR]
RewriteCond %{SERVER_NAME} =test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName test.com
ServerAlias www.test.com
SSLCertificateFile /etc/letsencrypt/live/test.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/test.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
This works perfectly for http -> https redirection. For example test.com or http://test.com redirects to https://test.com. www.test.com does not work (I don't know why?), I'll get a 404 page. Next big thing, lets assume the ip of my webserver is 12.23.45.67 - how do I have to change my config file, so this ip also redirects to https://test.com?
new .conf file based on #Don't Panic post:
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName test.com
ServerAlias www.test.com
RewriteEngine on
#RewriteBase /
RewriteCond %{SERVER_NAME} =www.test.com [OR]
RewriteCond %{SERVER_NAME} =test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerName XX.XX.XX.XX
ServerAlias www.test.com
DocumentRoot /var/www/html/webserver/public
RewriteEngine on
RewriteCond %{SERVER_NAME} =test.com [OR]
RewriteCond %{SERVER_NAME} =www.test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

So my final solution looks like this:
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
Redirect / https://test.com
</VirtualHost>
This is working for nearly every redirection, but it's not working for www.test.com - Is this a good solution? May not. But it's kind of a working one.

Related

Apache2 mod_rewrite not fetching .htaccess

Using a fresh install of Ubuntu 18.04 with Apache2 (using virtual hosts), however I cannot get .htaccess to rewrite my URL as needs be.
Consider:
000-default.conf:
<VirtualHost *:80>
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
allow from all
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
allow from all
</Directory>
ServerName mydomain.com
ServerAlias www.mydomain.com mydomain.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/radio/
</VirtualHost>
.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
Any help is much appreciated :)

Server not reading htaccess file and redirect not working

My site is not reading the htaccess file. Even if I add Deny from all at the top, the site loads fine.
Permission is 644. I have a number of sites running on the server so Apache is set up correctly.
This is my virtualhost config:
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/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>
This is what I'd added to .htaccess file to redirect www to non-www:
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
I tried adding it to virtualhost as well but it's still not redirecting.
After issuing an SSL Certificate with Let's Encrypt I had noticed it had created a duplicate conf file for port 443:
/etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost 12.34.56.78:443>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
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>
Prior to the LE, the original example.com.conf didn't have the AllowOverride All directive. So I assume what has happened is the contents were duplicated and then I only changed the original one.

Apache VirtualHost and Wildcard SSL

I am trying to configure my website so that :
www.example.com alias example.com is served by a VirtualHost
*.example.com are serverd by another one with different DocumentRoot
My concern is that the first item works well, but if I call site1.example.com, it still shows /home/default_domain content, not /home/blogs/site1
Here is the configuration:
Listen 443 https
<VirtualHost *:443>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /home/default_domain
[SSL configuration]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerName blabla.example.com
ServerAlias *.example.com
VirtualDocumentRoot /home/blogs/%1
[SSL configuration]
<Directory /home/blogs/>
Options MultiViews FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Redirect all unknown domains

I want to redirect all unknown subdomains in apache, can you help me ? Exemple : " aaa.mydomain.com / bbb.mydomain.com / ccc.mydomain.com " redirect to " all.domain.com ".
My current host :
>
ServerAdmin me#gmail.com
ServerName domain.dev
ServerAlias administration.domain.dev
ServerAlias agence.domain.dev
ServerAlias css.domain.dev
ServerAlias dev.domain.dev
ServerAlias dome.domain.dev
DocumentRoot /var/www/symfony/domain/web
<Directory /var/www/symfony/domain/web>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error_dev.domain.dev.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access_dev.domain.dev.log combined
You can do this with a reverse proxy, look at apache proxypass

rewrite rule for any subdomain to main domain

I have a site called mailcake.com, and I would like to have beta.mailcake.com, to redirect to mailcake.com, How can I achieve this? I have it like this on /etc/httpd/conf.d/mailcake.conf
NameVirtualHost *:443
<VirtualHost *:80>
DocumentRoot /var/www/mailcake
ServerName mailcake.com
ServerAlias www.mailcake.com beta.mailcake.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^beta\.mailcake\.com$ [NC]
RewriteRule (.+)$ "http://mailcake.com" [L,P]
ErrorLog /var/log/mailcake.com-error_log
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/mailcake
ServerName mailcake.com
ServerAlias www.mailcake.com
SSLEngine on
SSLCertificateFile /var/www/mailcake.crt
SSLCertificateKeyFile /var/www/mailcake.key
ErrorLog /var/log/mailcake.com-error_log
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
but it does not work
The following seems to work when tested with a few URLs:
RewriteCond %{HTTP_HOST} ^(beta\.)?mailcake\.com
RewriteRule (.*) http://mailcake.com/$1 [QSA]
Part of the important bit here is adding the $1 to ensure that any paths get added to the URL (in your example, http://beta.mailcake.com/stats/ would get redirected to http://mailcake.com/, which possibly isn't desireable. Also, adding [QSA] should ensure any query strings are passed along too.
If you're doing the redirect within the httpd.conf, I probably also wouldn't add [L], as it could stop other valid redirects within any .htaccess files from working.