redirect subdomains bar one - apache

So I've set up a Amazon EC2 and registerd a few domains with 123-reg, i've set up my Apache VirtualHost But now I'm going to need to set up a few sub domains (like kitten.example.com). But it's only going to be a few, and I would like the rest of the subdomain wild cards to go to the base like this:
kitten.example.com -> kitten.example.com BUT
*.example.com -> example.com
currently my DNS with 123-reg is as follows:
www A 198.168.0.0
kitten A 198.168.0.0
* A 198.168.0.0
and my Apache httpd.conf is:
<VirtualHost *:80>
DocumentRoot "/var/www/example.com"
ServerName example.com
ServerAlias example.com
<Directory "/var/www/example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/kitten.example.com"
ServerName kitten.example.com
ServerAlias kitten.example.com
<Directory "/var/www/kitten.example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
so how do i redirect all those wildecard subdomains to my base domain?

You will have to map DocumentRoot and Directory to your base domain directory.
or if you wish to change the domain name also, then write a .htaccess rule for it.

Create a virtual host directive that doesn't specify the ServerName or ServerAlias. This will catch all virtual hosts on that IP address (you specifcy all IP's on port 80) that do not map to any other virtual host specification.
<VirtualHost *:80>
DocumentRoot /www/default
...
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/examples.html#default
However, if you want to send a 301 HTTP Header (permanently redirected), you'll need to use mod_rewrite or something similar:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
How to setup Apache mod_rewrite to redirect all but one subfolder is one good example .. needs to be modified to suit your needs if the first option isn't sufficient for your needs.

Related

Apache VirtualHost configure

After I configured virtual host, my apache document root changed to the virtual host's document root, I just want to know why.
here is my httpd-vhosts.conf:
<VirtualHost *:80>
ServerName myapp.zend
DocumentRoot /opt/lampp/htdocs/php_zend_projects/myapp
<Directory /opt/lampp/htdocs/php_zend_projects/myapp/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</Directory>
</VirtualHost>
After I restart apache server, localhost page changed to index of /opt/lampp/htdocs/php_zend_projects/myapp,
http://gwjyhs.com/t6/702/1556725814x2728329017.png
but it is supposed to be xampp's default page like this:
http://gwjyhs.com/t6/702/1556726269x2728278877.png
If you enable vhosts you have to add an entry that looks like:
<VirtualHost *:80>
DocumentRoot "F:/Dev/xampp/htdocs"
ServerName localhost
</VirtualHost>
Note: change path to whatever is appropriate for you.
Restart webserver and it should work as before.
Reasoning behind this can be found in a comment on top of the httpd-vhosts.conf:
The first VirtualHost section is used for all requests that do not match a ##ServerName or ##ServerAlias in any block.
That means when you type in localhost it fallbacks to your myapp.zend vhost because it is (probably) the first virtualhost section.

Apache disable localhost redirection

i got a problem with apache conf with Laragon.
so the case is :
i got 2 subdomain
subdom1.mysite.com (DNS Record to IP 1.2.3.4)
subdom2.mysite.com (DNS Record to IP 1.2.3.4)
IP 1.2.3.4 is my VPS
everything went well, but i got a problem.
when i access 'localhost' from my VPS, it keeps redirecting to subdom1.mysite.com
and localhost/phpMyAdmin also redirected to subdom1.mysite.com/phpMyAdmin
here's my httpd.conf
Define APACHE_LOG_DIR "C:/mydir/logs"
Define APACHE_ROOT_WEB_DIR "C:/mydir/wwwroot"
ServerName localhost
DocumentRoot "C:/mydir/wwwroot"
<Directory "C:/mydir/wwwroot">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerName subdom1.mysite.com
ServerAlias subdom1.mysite.com
DocumentRoot ${APACHE_ROOT_WEB_DIR}\subdom1.mysite.com
ErrorLog ${APACHE_LOG_DIR}/subdom1.mysite.com-error.log
CustomLog ${APACHE_LOG_DIR}/subdom1.mysite.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName subdom2.mysite.com
ServerAlias subdom2.mysite.com
DocumentRoot ${APACHE_ROOT_WEB_DIR}\subdom2.mysite.com
ErrorLog ${APACHE_LOG_DIR}/subdom2.mysite.com-error.log
CustomLog ${APACHE_LOG_DIR}/subdom2.mysite.com-access.log combined
</VirtualHost>
my localhost keeps redirecting to first vHost. I've googled lot's of combination of directory, serverName etc. But no luck.
Your VirtualHosts are binding to all available interfaces. Because of how apache determines to which VHost should route a request, it's ending up in the first one defined.
You can either:
Bind your subdomains to the external ip only.
Create a VirtualHost for the main server using the loopback ip.
Create a VirtualHost with the _default_ address.

apache 2.4 serverAlias with * wildcard overwrittes specificed vhost

I have two vhosts the default one and another one:
<VirtualHost *:80>
ServerName www.eve-stuff.com
ServerAlias *.eve-stuff.com
DocumentRoot /var/www/html/test
<Directory /var/www/html/test>
Options +FollowSymLinks
AllowOverride None
#Require all denied
</Directory>
</VirtualHost>
This one should just redirect all subdomains that a not specifically setup to the test directory.
This is another vhost i have:
<VirtualHost *:80>
ServerName dev.eve-stuff.com
DocumentRoot /var/www/html/dev
<Directory /var/www/html/dev>
Options +FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
This one should show the dev folder for the dev subdomain.
From what I understood from the apache doc the ServerAlias *.eve-stuff.com in the first host will always catch UNLESS another vhost has specifically been created for the address.
However dev.eve-stuff.com still redirects to the test folder not to the dev folder, as it should.
All dns entries, also for the subdomains, point to the right IP.
I am running Apache/2.4.18 (Ubuntu).
Turns out the order in which it is setup matters.
Apache apparently looks for the matching host it can find.
Since the first config was in 000-default.conf and the second one was in the 020-dev.conf the dev-subdomain matched the first vhost and used that.
I changed default to 999-default.conf which means now dev will first be matched with the proper vhost but other undefined subdomains will still be matched with the default vhost.

Configure Apache to run website off of port-enabled IP address

To be perfectly honest, I'm not even sure if this is doable...
I've configured my vhosts file in /etc/apache2/sites-enabled which you can see here:
<VirtualHost 159.203.171.140:8080>
ServerAdmin webmaster#localhost
ServerName 159.203.171.140:8080
DocumentRoot "/home/wiki/public_html"
DirectoryIndex index.php index.html
<Directory "/home/wiki/public_html">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/wiki_error.log
CustomLog ${APACHE_LOG_DIR}/wiki_access.log combined
</VirtualHost>
for a digitalocean droplet given at the IP listed in the above hosts file. This droplet has absolutely nothing on it except for the wiki user in /home/ plus the required php, mysql/mariadb, apache stuff.
What I want to be able to do is to go to 159.203.171.140:8080 and see my site without having to purchase a useless domain name.
I'd really appreciate some help with this one.
If you have only one website on the droplet, then you don't need to set up a virtual host. You can use the 000-default.conf, no need for a2ensite.
You do not need the ServerName, which won't work with the IP as a name, you also don't need the IP address in the VirtualHost directive.
So, instead of this:
<VirtualHost 159.203.171.140:8080>
ServerAdmin webmaster#localhost
ServerName 159.203.171.140:8080
DocumentRoot "/home/wiki/public_html"
...
You can use this in your 000-default.conf file
<VirtualHost *:8080>
DocumentRoot "/home/wiki/public_html"
...
The rest of the directive stays as you have it.
Also, one note, if you are using port 8080, then you need go to /etc/apache2/ports.conf and set the Listen to 8080 (restart Apache after doing this).

I want to make a separate domain for images

I want to set up a domain called img.mydomain.com. It will be a virtual domain that just like my actual domain except for one difference: it will only serve files that end in .jpg .jpeg .gif .png etc. This way I can refer to img.mydomain.com/some_image.jpg. it will speed up the page speed by making the browser think that it's two separate domains (google page speed analyzer is always nagging me to do this).
I'm running apache on a linux server. Should I do this in httpd.conf? If so, what is my first step?
create 2 folder for each domains (for example):
/var/www/domain.com
/var/www/img.domain.com/
here's what you can put in your httpd.conf
<VirtualHost *:80>
DocumentRoot "/var/www/domain.com"
ServerName domain.com
ServerAlias domain.com www.domain.com
<Directory "/var/www/domain.com">
allow from all
Options +Indexes
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/img.domain.com/"
ServerName img.domain.com
ServerAlias img.domain.com
<Directory "/var/www/img.domain.com/">
allow from all
Options +Indexes
</Directory>
</VirtualHost>