Apache VirtualHosts multiple ServerAliases with different TLDs - apache

I am working with several domains which all follow a similar pattern of redirects. Instead of writing out each domain as it's own virtual host, I am attempting to make the file more maintainable using only one:
<VirtualHost *:80 *:443>
ServerName domain.xz
ServerAlias *.domain.xx *.domain.xy
RewriteEngine on
RewriteRule ^/(.*)$ https://xz.newdomain.com/$1 [QSA,NC,L,R=301]
</VirtualHost>
In the above example domain.xx will redirect to xz.newdomain.com correctly however domain.xy will not. I have checked the documentation and cannot determine the reason that this does not work.
Is it possible to make this work as I intend or will I have to make them separate VirtualHost configurations?

You should add also second level domains names in ServerAlias, i.e:
ServerAlias domain.xx *.domain.xx domain.xy *.domain.xy

Related

Set urls for redirect to a specific url without rewrite

I'm building a webscheduler that have a multitenant structure, what I'm trying to do is assign a custom url that point to my application to each buyer.
So essentially when a user buy a license from me, I'll create a custom url on my webserver like this:
http://webserver/foo.scheduler.com/login
where foo is the name of the user that has buyed the license, and scheduler is a default part of the url, another example with more buyers:
http://webserver/foo.scheduler.com/login
http://webserver/foo2.scheduler.com/login
http://webserver/foo3.scheduler.com/login
essentially there are three buyers (my customers), each custom endpoint allow me to identify the correct database credentials, 'cause in my logic each tenant have a specific db, for more data organization.
Actually my application is located to this endpoint:
http://webserver/scheduler
I want to know if is possible point all custom urls to http://webserver/scheduler, without rewrite the url in the browser, so for example when the user go to http://webserver/foo.scheduler.com/login in the true is http://webserver/scheduler/login, but the user still continue to see http://webserver/foo.scheduler.com/login.
How can do this? In my .htaccess, available inside the root of the application folder I've this content:
RewriteEngine On
RewriteBase /webscheduler/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
this allow me to rewrite the base path to the index, and shunt the trace to the specific controller.
Happy to help you with this.
Get a valid SSL certificate for *.scheduler.com. You are going to need that if you're going to get this to work. Are you sure you want to use HTTPS? Your other URL is not HTTPS. Then you will need to set up your virtual host for *.scheduler.com to work properly with that certificate. Only having:
<VirtualHost *:443>
ServerAlias *.scheduler.com
DocumentRoot "/var/www/html/progetti/scheduler"
</VirtualHost>
Is not going to be anything like enough. You need all the mod_ssl stuff setting up in there as you have with the other virtual host. You could just use that default HTTPS host instead of adding another one, and modify it.
The first thing to do is get your hosting working for https://*.scheduler.com/ and then just point it at the right place.
What do you mean your endpoint is http://webserver/scheduler? This is not a valid domain name. Please clarify what you mean by that and I will update my answer with more information. Is the code on the same server?
--
Update
So to do this without SSL, add the following to your "000-default.conf", after what is currently there:
<VirtualHost *:80>
ServerAdmin localhost#gmail.com
ServerName www.scheduler.com
ServerAlias *.scheduler.com
UseCanonicalName off
DocumentRoot /var/www/html/progetti/scheduler
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/progetti/scheduler>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
--
Update
To make http://webserver/foo.scheduler.com work and serve /scheduler, add this to the VirtualHost that was already there. Not the new one added above, the original one at the top.
RewriteEngine on
RewriteRule ^(/[^./]+\.scheduler\.com)(?:$|/(.*)$) /scheduler/$2
Let me know any problems. If you would prefer to put it in your .htaccess it will need updating.
Note: I'm taking literally your statements that the app is using http:// and the clients will use https:// URLs. Also I'm assuming that the clients are hitting the same server as the one that hosts the app.
Probably the simplest way to do this is to configure one VirtualHost for your actual application and a separate one for the other URLs.
So assuming your application lives in /var/www/html/scheduler, then your existing VirtualHost looks like:
<VirtualHost *:80>
ServerName webserver
DocumentRoot "/var/www/html"
</VirtualHost>
You would need to add change your conf.d/ssl.conf to have something like:
NameVirtualHost *:443
<VirtualHost *:443>
ServerAlias *.scheduler.com
DocumentRoot "/var/www/html/scheduler"
</VirtualHost>

Rewrite spare domains to main domains with .htaccess

We have a site with an English and Spanish version, each on a different domain. We also have a few spare domains for each language which we'd like to redirect to the language's main domain.
Specifically:
estadiosfutbol.net/..., estadiosfutbol.org/... and estadiosfutbol.info/... should all redirect to https://estadiosfutbol.com/...
worldfootballstadiums.com/..., worldfootballstadiums.info/..., worldfootballstadiums.org/... and worldfootballstadiums.net/... should all redirect to https://worldstadiums.football/...
I'm struggling with the rewrite rules so any help would be greatly appreciated.
There are two ways this can be done. The first is the simpliest, but is not always practical.
First Method
This method does not require HTACCESS files. In your Apache server configuration you just need to add ServerAliases for each of the domains that you want it to handle. (You must make sure all the domains are pointing at the same machine)
The Code
NameVirtualHost *:443
<VirtualHost *:443>
ServerName estadiosfutbol.com
ServerAlias estadiosfutbol.info estadiosfutbol.net estadiosfutbol.org
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:443>
ServerName worldstadiums.football
ServerAlias worldfootballstadiums.com worldfootballstadiums.net worldfootballstadiums.info worldfootballstadiums.org
DocumentRoot /www/otherdomain
</VirtualHost>
Note: This will only redirect if the user tries to access the website using SSL. (eg ) If you want it to redirect all traffic from both port 80 and port 443 you would need to make separate virtual hosts and use the second method to achieve the redirection.
Second Method
The second way is a little more complicated, but works in almost all situations. There a two main steps that need to be carried out in order for this to work properly:
Make sure that whatever server software you are using is setup to be looking for all the domains. The server has to have a VirtualHost(Apache) that is listening for each domain in order for the next step to do anything.
Create a .HTACCESS file under each domains' root that looks similar to this:
The Code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !estadiosfutbol.net$ [NC]
RewriteRule ^(.*)$ https://estadiosfutbol.com/$1 [L,R=301]
Note: You will need to change the third line on each domain to be the domain to rewrite from (eg estadiosfutbol.net/, estadiosfutbol.org/ and estadiosfutbol.info)
Note: Changing the forth line is all that is required for the separate domain.

Redirect but keep the domain the same

I have an owncloud server, and I would like to setup a second short domain, to keep the shared links short.
lets say we've the longdomain.com and short.com
Heres is my httpd lines
<VirtualHost *:80>
ServerAdmin email#adderss.com
DocumentRoot /var/www/dir/public_html
ServerName short.com
ServerAlias www.short.com
RewriteEngine on
RewriteRule ^/([A-Za-z0-9]{4,12})$ https://www.long.domain.com/public.php?service=shorty_relay&id=$1 [QSA,L]
ErrorLog /var/www/dir/error.log
</VirtualHost>
With the current lines, short.com redirects to exactly where I need, but I would like this redirect to be on the background and keep the short domain on the user's browser.
How can I do this?
Update:
with this in my short domain virtual host I can visit my owncloud using the short domain.
For example: short.com/index.php/apps/files/
the long domain is vanished. I think Im one step forward now.
ProxyPass / https://www.long.domain.com
ProxyPassReverse / https://www.long.domain.com
The next step is to use the the regex so I can load only shorty id links.
How can I combine the rewrite regex above with the proxypass.
I've tried ProxyPassMatch butI havent figured out how to use it properly
Any ideas?
If you can't make the same content addressable by short.com, short.com can proxy to the long domain by loading mod_proxy, mod_proxy_http, and changing your rewrite flag from R to P.

Mass virtual hosting with Apache 2.4

I would like to use the value set by VirtualDocumentRoot in a RewriteRule.
The Apache documentation says:
The other thing to determine is the document root (configured with DocumentRoot and available to CGI scripts via the DOCUMENT_ROOT environment variable). In a normal configuration, this is used by the core module when mapping URIs to filenames, but when the server is configured to do dynamic virtual hosting, that job must be taken over by another module (either mod_vhost_alias or mod_rewrite), which has a different way of doing the mapping. Neither of these modules is responsible for setting the DOCUMENT_ROOT environment variable so if any CGIs or SSI documents make use of it, they will get a misleading value.
Refering to the documentation, it seems I won't be able to use %{DOCUMENT_ROOT} in RewriteRule. But after looking for alternative solutions, I found this ticket (title: DOCUMENT_ROOT environment variable set incorrectly with VirtualDocumentRoot) saying it has been “fixed in 2.4.1”.
I am using the latest (stable) version of Apache:
$ httpd -v
Server version: Apache/2.4.9 (Unix)
Server built: Jun 18 2014 03:07:48
... but it doesn't seem to work for me. When testing, %{DOCUMENT_ROOT} has the default value set by DocumentRoot in httpd.conf. I would like %{DOCUMENT_ROOT} (or any other variable I could use) to be dynamically set by VirtualDocumentRoot.
Here is what I would like to use in httpd-vhosts.conf:
<VirtualHost *:8080>
ServerName dev
ServerAlias *.dev
VirtualDocumentRoot "/usr/local/var/www/.dev/%-2+"
RewriteRule ^/(.*\.php(/.*)?)$ "fcgi://127.0.0.1:9000%{DOCUMENT_ROOT}/$1" [P]
</VirtualHost>
Is there a solution?
After having looked in many directions with #John Auld (thanks for your time and help), I have finally found a solution. As I said in a previous comment, it is not 100% perfect but good enough for me.
For clarity's sake, here is what I wanted to have in httpd-vhosts.conf:
<VirtualHost *:8080>
ServerName dev
ServerAlias *.dev
VirtualDocumentRoot "/usr/local/var/www/.dev/%-2+"
RewriteRule ^/(.*\.php(/.*)?)$ "fcgi://127.0.0.1:9000%{DOCUMENT_ROOT}/$1" [P]
</VirtualHost>
And here is what I've come up with:
<VirtualHost *:8080>
ServerName dev
ServerAlias *.dev
VirtualDocumentRoot "/usr/local/var/www/.dev/%-2/%-3"
RewriteCond %{SERVER_NAME} (?:([^.]+)\.)?([^.]+)\.dev
RewriteRule ^/(.*\.php(/.*)?)$ "fcgi://127.0.0.1:9000/usr/local/var/www/.dev/%2/${:%1|www}/$1" [P]
</VirtualHost>
I use RewriteCond to capture parts of %{SERVER_NAME} and reuse them in RewriteRule.
There are two capturing groups: subdomain (%1) and domain (%2). The subdomain is optional so I use the syntax ${map:value|default} to set a default value for the subdomain when empty.
VirtualDocumentRoot has its own default value when a part (%N.M) is not available. As said in the documentation:
If N or M is greater than the number of parts available a single underscore is interpolated.
That's why I had to use a little "trick" to make it work with an optional subdomain:
ln -sfv /path/to/the/project/www /path/to/the/project/_
If you think of a way to do it "config only", leave a comment and I will edit my answer with your contribution.

Redirect subdomain to subdirectory

i've already read all topics here and on google about redirection but i'm a programmer and i cant get it going.
I have Apache 2.2 installed. The web root is C:\Apache\htdocs. My network admin set me up a local domain that points to the server with Apache. The domain is myPhpApp.ourcompany.local. And this subdomain works, it shows the Apache "It works" page.
Now i have a website in C:\Apache\htdocs\myPhpApp and i want Apache to redirect the myPhpApp.ourcompany.local to this directory. The URLs should stay while browsing the website always as myPhpApp.ourcompany.local for example: myPhpApp.ourcompany.local/index.php, myPhpApp.ourcompany.local/data.php and so on.
I dont know how to achieve this? Mod-rewrite, virtual hosts, combination of both?
i have got this and this does not work:
<VirtualHost myphpapp.ourcompany.local>
DocumentRoot /myphpapp/
ServerName www.example1.com
RewriteEngine on
RewriteRule ^/$ /myphpapp/ [R]
</VirtualHost>
In effect i get:
Forbidden
You don't have permission to access
/myphpapp/ on this server.
Can anyone help?
EDIT
Maybe i forgot to mention: i dont put this into the www root which is C:/Apache/htdocs becuase i have more apps in there.
i have 3 directories in thdocs: myphpapp, myoldapp, mytestapp. As a target i want to have 3 subdomains that point to each directory.
I think it will be a combination of Nikola's and cromestant's answers:
<VirtualHost *:80>
DocumentRoot "C:/Apache/htdocs/myPhpApp"
ServerName myPhpApp.ourcompany.local
</VirtualHost>
First of all, you don't need a rewrite rule for simple thing as this.
I assume you need following VirtualHost definition
<VirtualHost *>
DocumentRoot /myphpapp/
ServerName myphpapp.ourcompany.local
</VirtualHost>
"VirtualHost *" part tells Apache on which interface and optionally port to listen to.
ServerName tells which domain name will be used to identify this virtual host.
Document root in your apache config should point to your directory where you have the app, in what you stated in your question it should be
DocumentRoot C:\Apache\htdocs\myPhpApp
and that is all.
restart or reload your apache, and test.