Wild card Sub-domain using virtual hosts - apache

Can someone explain to me how this actually works. This is the code in Apache's httpd.conf file.
<VirtualHost *:80>
DocumentRoot "somepath"
ServerAlias mobile.mysite.com
ServerName mobile.mysite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "somepath"
ServerAlias *.mysite.com
ServerName mysite.com
</VirtualHost>
I want do something like if its mobile.mysite.com it should go to mobile.mysite.com. If its anything else.mysite.com, then it should go to mysite.com. Does the above code perform the same thing ? Any suggestions ?

No, this code does not do any of the mentioned task. You will have to use mod_rewrite for this purpose.
Modify your httpd.conf file to include mod_rewrite.so and then write something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule $ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
What the above rewrite rule does is that it checks whether the host name starts with www or not, if it does not then it includes www. in the hostname and redirects.
You could write a rewrite rule similar to this.
Read more about rewrite rule here

Related

Apache virtual host with regex

I have one domain: www.abc.com
I want to use Apache virtual host to get this:
www.abc.com/index.html?id=m1 ,www.abc.com/index.html?id=m2
to visit
/home/m/index.html
and
www.abc.com/index.html?id=a1 ,www.abc.com/index.html?id=a2
to visit
/home/a/index.html
Can I use apache regex to complete this without .htaccess, like this:
<VirtualHost (www.abc.me/*?id=a*):80>
DocumentRoot /var/www/html/laohu_v1
ServerName www.abc.me
ServerAlias www.abc.me/*?id=a*
</VirtualHost>
First, you only need or should use .htaccess when you don't have full access to your server. Vhost doesn't require to be put in .htaccess. Our Apache config uses none at all ;)
Here is how your vhost could look like. But if you only host one domain with entrance over one port on your server, leave out the <VirtualHost> directive and put the rewrite directly in your server's config.
<VirtualHost *:80>
DocumentRoot /var/www/html/laohu_v1
ServerName abc.me
ServerAlias www.abc.me
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)\d$
RewriteRule ^(.*) /home/%1/index.html
</VirtualHost>
RewriteCond %{QUERY_STRING} ^id=(.*)\d$ change that to (.) if only ONE character is in front of the digit. And if you need more than one digit, change that to: \d+

Apache Settings For Unique subdomain and Domain Directories

I'm fairly capable with most of the web server configuration but I have run into a problem.
I have a VPS install with one sub domain. Currently the link sub1.domain.com points to the root of the public_html/www folder.
I am now looking at creating new sub domains as well as using domain.com to point to a wordpress installation.
From my understanding, I must edit the apache httpd.conf file, but I am unsure exactly how to do this. I would expect that I would have to have domain.com point to a subdirectory as well as sub2.domain.com point to a different sub directory. Something like the following.
sub1.domain.com >> rootPathForApache/
domain.com >> rootPathForApache/domain.com/
sub2domain.com >> rootPathForApache/sub2/
Could somebody please point me in the right direction.
Kind Regards,
You have a couple options:
Option 1: Apache's <VirtualHost> directive could solve this (multiple). This is only available in the server config context.
<VirtualHost *:80>
DocumentRoot "rootPathForApache/"
ServerName sub1.domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "rootPathForApache/domain.com/"
ServerName domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "rootPathForApache/sub2/"
ServerName sub2domain.com
</VirtualHost>
Option 2: Use a .htaccess file in rootPathForApache with the mod_rewrite module loaded, and introduce ServerAlias directive into either your existing <VirtualHost> directive.
.htaccess contents (or if you want within the VirtualHost directive itself):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1.domain.com
RewriteRule ^(.*) $1 [L]
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*) domain.com/$1 [L]
RewriteCond %{HTTP_HOST} ^sub2domain.com
RewriteRule ^(.*) sub2/$1 [L]
...and the ServerAlias:
ServerName domain.com
ServerAlias sub1.domain.com sub2domain.com
The latter solution (option 2) just seems ridiculous, especially since ServerAlias is only available within a VirtualHost directive and mod_rewritehtaccess files add unnecessary overhead. That and it doesn't seem natural. I guess it illustrates the flexibility of Apache's configuration and modules.

How to redirect a web page while not changing the URL (using Apache VirtualHost)

I'm setting up Virtual Hosts in my Apache web server and already have rules in place that just do a simple 301 redirect from one URL to another. However, I've now been asked if I can write a rule that redirects to another page while keeping the URL the same and I've tried this:
<VirtualHost *:80>
ServerName ZZZ.com
ServerAlias YYY.com ZZZ.com
Redirect / YYY.com
</VirtualHost>
And this:
<VirtualHost *:80>
ServerName ZZZ.com
RewriteEngine on
RewriteRule ^/(.*) YYY.com/$1 [R]
</VirtualHost>
Neither did what I expected of them. They look wrong to me but I'm just not finding any helpful information anywhere. I've looked at http://httpd.apache.org/docs/2.4/vhosts/examples.html and http://httpd.apache.org/docs/current/mod/mod_rewrite.html - Neither of them were very helpful.
<VirtualHost *:80>
ServerName YYY.com
ServerAlias ZZZ.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^yyy\.com$ [NC]
RewriteRule (.*) YYY.com$1 [R=302,L]
</VirtualHost>
You were missing a condition to prevent it from redirecting correct urls.

Redirect address with folder structure to https?

I want to make sure all my traffic is on ssl even if they type http. But I also want it to pass the folders so mod_rewrite will still work. I tried this poor example but it does not work. Basicly I if they type http://mydomain.com/apage it will redirect to https://mydomain.com/apage
Server: Apache2, LAMP stack.
.htaccess
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(/) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
I am open to tweaking a virtual host files for Apache but I have not seen it done like that before. This is my first adventure into ssl hosting.
Just replacing http with https
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I suggest not using mod_rewrite or htaccess if you have access to httpd.conf.
If you want to force all users to use https (a good idea) you can add something like this to httpd.conf:
<VirtualHost 1.2.3.4:80>
ServerName SSL.EXAMPLE.COM
CustomLog /var/log/httpd/EXAMPLE.access_log combined
ErrorLog /var/log/httpd/EXAMPLE.error_log
Redirect / https://ssl.example.com/
</VirtualHost>
<VirtualHost 1.2.3.4:443>
ServerName ssl.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite HIGH:MEDIUM
.
.
.
</VirtualHost>
<Directory /var/www/html>
#If all else fails, this will ensure nothing can get in without being encrypted.
SSLRequireSSL
</Directory>

Variable/Dynamic/REGEX Virtualhost in Apache?

Just an off the wall question today. Is it posible to vary DocumentRoot of a virtualhost based on the subdomain requested like so?
<VirtualHost *>
ServerName ^VARIABLE$.example.com
DocumentRoot ~/Sites/^VARIABLE$
</VirtualHost>
Yes it is possible:
Step1: Setting up Wildcard DNS
You have to add an A Record that points to your server's IP like that:
*.example.com. IN A 192.168.1.1
Step2: Set up apache VirtualHost
<VirtualHost *>
ServerName www.example.com
ServerAlias *.example.com
DirectoryIndex index.html
DocumentRoot /home/www/www.example.com/htdocs
....
</VirtualHost>
Notice the important line: ServerAlias *.example.com. This will tell Apache that any host with the .example.com suffix will match this virtual host too.
Step3: Setting up Rewrite Rules
You have to add this lines in your .htaccess file located in your web root folder (eg. /home/www/www.example.com/htdocs):
RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.example.com [NC]
RewriteCond %{http_host} ^([^.]+)\.example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1/ [R=301,L,QSA]
That way a request for foo.example.com will redirect visitors to example.com/foo and so on. Good luck.
(Reference: http://www.debian-administration.org/articles/358)