Redirect all unknown domains - apache

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

Related

Redirect ip address to https server name

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.

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.

Redirect Error: Wordpress + Let's Encrypt (Certbot) + SSL only + non-www

Note: See the updates at the end of this post. For the final (working) conf-files see update 4 at the end of this post or the post which I marked as the solution.
I badly configured my apache conf-files and now I'm getting a redirect error (ERR_TOO_MANY_REDIRECTS). I want to redirect everything to HTTPS (non-www). I already tried to add this to the wp-config.php regarding this tips, but that didn't solve the problem:
define('WP_HOME','http://d0main.xyz');
define('WP_SITEURL','http://d0main.xyz');
and I tried to add
define('WP_HOME','https://d0main.xyz');
define('WP_SITEURL','https://d0main.xyz');
Here are my Apache files:
d0main.xyz.conf
<VirtualHost *:80>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin contact#d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
d0main.xyz-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin d0main#d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
SSLCertificateFile /etc/letsencrypt/live/d0main.xyz/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/d0main.xyz/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</VirtualHost>
</IfModule>
<IfModule mod_rewrite.c>
Update 1: I solved the redirection error by removing Redirect permanent / https://d0main.xyz in my d0main.xyz-le-ssl.conf, but now there is a / missing at the end of the URL. Graphic URL's for example are now https://d0main.xyzwp-content/image.jpg
Update 2: This is getting more weird. I changed the line Redirect permanent / https://d0main.xyz to Redirect permanent / https://d0main.xyz\/ in my d0main.xyz.conf. Now some images have two slashes (and work) https://d0main.xyz//wp-content/uploads/2016/10/logo-5.png, while other images still have no slash in their URL: https://d0main.xyzwp-content/uploads/2016/10/image2.png
Update 3: I forgot to post my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Update 4: My final (working) conf-files:
d0main.xyz.conf
<VirtualHost *:80>
ServerName d0main.xyz
ServerAlias www.d0main.xyz
ServerAdmin contact#d0main.xyz
DocumentRoot /var/www/html
Redirect permanent / https://d0main.xyz/
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
d0main.xyz-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName delegatex.xyz
ServerAlias www.delegatex.xyz
ServerAdmin delegatexyz#gmail.com
DocumentRoot /var/www/html
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
SSLCertificateFile /etc/letsencrypt/live/delegatex.xyz/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/delegatex.xyz/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
<IfModule mod_rewrite.c>
You do not need to escape the forward slash, so instead of Redirect permanent / https://d0main.xyz\/ you should just have Redirect permanent / https://d0main.xyz/. But when redirecting (and ProxyPassing) you should always match trailing slashes.
Instead of the following lines.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
You should have a SSL vhost for www.d0main.xyz and jsut do Redirect / https://d0main.xyz/
If you are still getting the double slashes it looks like the reason must be elsewhere. DO you have any htaccess files?

This request asked for "/" on host "example.com", but no site is configured which can serve this request

I am using Apache/2.4.7 to host a server. I am hosting phabricator on it.
Let us say that the ip is x.x.x.x and the domain name pointing to the ip is example.com.
Following are the contents of
/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerName http://x.x.x.x/
DocumentRoot /home/ubuntu/phabricator/phabricator/webroot
RewriteEngine on
RewriteRule ^/rsrc/(.*) - [L,QSA]
RewriteRule ^/favicon.ico - [L,QSA]
RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA]
<Directory /home/ubuntu/phabricator/phabricator/webroot>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName http://example.com
ServerAlias www.example.com
DocumentRoot /home/ubuntu/phabricator/phabricator/webroot
RewriteEngine on
RewriteRule ^/rsrc/(.*) - [L,QSA]
RewriteRule ^/favicon.ico - [L,QSA]
RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA]
<Directory /home/ubuntu/phabricator/phabricator/webroot>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
So, if I enter the ip, i.e., x.x.x.x in the address bar of the browser, phabricator opens up nice and fine. But instead if I use the domain name: example.com,
it throws up the error:
This request asked for "/" on host "placire.com", but no site is configured which can serve this request.
What mistake am I making? How do I go on about resolving it?
Thank you.
PS: I used the follwoing guide for configuring Phabricator:
https://secure.phabricator.com/book/phabricator/article/configuration_guide/
PPS: Also, I'm pretty sure that example.com points correctly to x.x.x.x because using the following conf file , when I enter example.com in the address bar I see the apache default page:
<VirtualHost *:80>
ServerName http://x.x.x.x
DocumentRoot /home/ubuntu/phabricator/phabricator/webroot
RewriteEngine on
RewriteRule ^/rsrc/(.*) - [L,QSA]
RewriteRule ^/favicon.ico - [L,QSA]
RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA]
<Directory /home/ubuntu/phabricator/phabricator/webroot>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This link: https://secure.phabricator.com/T8717 gives me a hint that you probably need to set the config parameter phabricator.base-uri. Try:
./bin/config set phabricator.base-uri 'http://example.com/'