How to retrieve server alias in Apache config? - apache

I have a single named virtual host that handles traffic for a bunch of different hostnames.
In the Apache config, I need to create RewriteRules according to the hostname requested by the user. Unfortunately the contents of the %{SERVER_NAME} variable always matches the primary hostname defined as ServerName in the config, instead of the alias requested by user.
e.g.: I need to serve site.company.com, site2.company.com and site3.company.com
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site.company.com
ServerAlias site?.company.com
RewriteCond %{SERVER_NAME} pattern
RewriteRule blah_depending_on_hostname_requested_by_user
</VirtualHost>
Problem: %{SERVER_NAME} is always site.company.com, even if user requests one of the aliases such as site2.company.com.
How do I solve this? There doesn't seem to be a %{SERVER_ALIAS} variable available.

You probably mean %{HTTP_HOST}. That's the exact site name provided by the browser. From the manual:
If you wish to match against the hostname, port, or query string, use
a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or
%{QUERY_STRING} variables respectively.

Related

How to skip HTTPS redirection, only if the url contains a substring?

In my Apache http.conf file, I have the following redirect setup, which means that if I access an endpoint using my server's domain name, it serves its HTTPS equivalent, instead of HTTP. However there's a particular URL where I do not want this address to occur. This is how i've got it set up at the moment:
<VirtualHost *:80>
ServerAdmin me#mydomain.com
DocumentRoot "/srv/httpd"
ServerName example.com
ServerAlias example.com
RewriteEngine On
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
So what do I need to add, so that if I were to access http://example.com/insecure/ (and anything within that folder), it doesn't redirect me anywhere, however it does redirect to HTTPS if i'm accessing anything else on my domain? Just a second RewriteCond? I'm not sure of the syntax
I've added a second RewriteCond of RewriteCond %{REQUEST_URI} !/insecure/ which seems to have done the job.

Apache settings, multi vhost multi servername same directory

I would like to know if it s possible to use the same block to set up different servername for one vhost. I have an application that is reachable by www.extranet.com i would like to access it through any sub domain like www.exemple.extranet.com without having to declare them all.
I could use server alias but i need to keep the original URI on the browser.
Any Thoughts ?
Probably the best method would to be to use the rewrite rules. For example:
RewriteEngine on
RewriteCond %{HTTP_HOST}^mydomain\.com [NC]
RewriteRule ^/(.*)$ http://www.mydomain.com [r=301,L]
This is the main use-case for ServerAlias. The vhost responds to not only the Servername, but also all ServerAliases. The only thing you need to "declare" is the list of hosts that this vhosts answers to.
ServerName example.net
ServerAlias www.example.net www.subdomain.example.net anothersubdomain.example.net
The hostname in the browser will stay the same.
Unless you do something about it with RewriteRules and such, but your goal was to not alter it. So the default behaviour should work for you.

Apache VirtualHosts

If apache was presented with the domain name origin.datingasia.co would it match both below VirtualHost entries?
<VirtualHost *:80>
ServerName datingasia.co
ServerAlias www.datingasia.co
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/datingasia.co
ServerName origin.datingasia.co
ServerAlias origin.datingasia.co
</VirtualHost>
Also once a domain has the rewrite rule applied - eg: has 'http://www.etc.etct' added to it does it redirect automatically? would this occur before a DocumentRoot path would be used?
Only the second entry would be triggered for the domain "origin.datingasia.co". If you wanted the first one to trigger it as well, you would need to add a second ServerAlias parameter.
ServerAlias origin.datingasia.co
This would cause a problem though since your second entry contains that same ServerAlias. All ServerName/ServerAlias must be unique, otherwise Apache will not know which block to use for the request.
The first entry will only catch requests for "datingasia.co" and "www.datingasia.co". You're missing the DocuemntRoot though, in the event that the rewrite rule condition is not met (IE: www.datingasia.co). This will cause your requests to fail once they get to the www.datingasia.co page because Apache will not know what root to serve the requests from. The browser will automatically be redirected to "www.datingasia.co" when they visit "datingasia.co".
For the second entry, you do not need "ServerAlias origin.datingasia.co" since you already have that domain defined in the ServerName. You would only need a ServerAlias line if you wanted an additional unique domain to point to this host (IE: ServerAlias www.origin.datingasia.co).
Since your rewrite rules are in the host, if the condition is met Apache will not need the DocumentRoot. But if the condition is not met, Apache will attempt to serve the request and as a result will require the DocumentRoot. Your rule will forward "datingasia.co" to "www.datingasia.co" but will fail at "www.datingasia.co" because it is missing the DocumentRoot and does not meet the RewriteCond.

apache - redirect subdomain to another domain

I need to redirect every subdomain into the domain it belongs changing the subdomain name into a parameter using mod_rewrite, and I'm not sure how. Also, I need to "reindex" the parameters so that the subdomain name becames the first parameter of the uri and the other parameters of the uri follow it by their own order. Something like this
category.domain.com/search/flowers
to
domain.com/category/search/flowers
Any thoughts on how to achieve this using mod_rewrite?
Cheers!
You can do this with one VirtualHost for all of the subdomains:
<VirtualHost *:80>
ServerName category.domain.com
ServerAlias foo.domain.com bar.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*).domain.com
RewriteRule (.*) http://domain.com/%1$1 [R=301,QSA,L]
</VirtualHost>
For it to work properly, you must have something set as the ServerName, so just choose one and list the rest of your subdomains on the ServerAlias line.
You can have multiple ServerAlias lines, so you could break them into multiple lines for readability if you have a large number of subdomains.
In the RewriteRule, the %1 matches the first matched pattern in preceding RewriteCond lines.

redirecting www.subdomain.example.com to subdomain.example.com

I've had some users trying to access a site that is registered as subdomain.example.com with www.subdomain.example.com.
is there some sort of .htaccess rule I can add to redirect people that arrive using www.subdomain.example.com to subdomain.example.com?
Also, do I have to change DNS stuff?
Sure, use a directive like:
<VirtualHost *:80>
ServerName www.subdomain.example.com
Redirect permanent / http://subdomain.example.com/
</VirtualHost>
Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).
Also, yes you will need to change DNS records, because www.subdomain.example.com is a distinct hostname that needs its own A (or CNAME) record to point the browser to an appropriate server in the first place.
RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com
RewriteRule (.*) http://subdomain.domain.com/$1 [R=301,L]
You need to add a virtual host directive in httpd.conf and Redirect Permament to the correct subdomain and add the additional DNS entry (CNAME is fine)