How can I write the Apache configuration two different sites? - apache

How can I write the Apache configuration to make two different sites on the same domain through a slash
Example:
site.ru - 1 site,
site.ru/app - 2 site

In Apache configuration, you should specify full URL without the part which goes after the slash, i.e.:
ServerName "site.ru:80"
It is not possible to define site.ru/app as a separate virtual host.
However, you can simply move the content of the second website to a subdirectory of the first one. For example, if:
ServerName "site.ru:80"
DocumentRoot "/var/www/httpdocs"
Then /var/www/httpdocs/app will be the directory with the content of the second website.
Another option is to create two virtual hosts, and then to add a rewrite rule to the first domain configuration.
Let's take two domains: site.ru and siteapp.ru. Requests to site.ru/app can be redirected to siteapp.ru using the following on site.ru:
RedirectMatch 301 ^/app/(.*)$ http://siteapp.ru/$1

Related

Does the Redirect directive override configured Aliases

I'm setting up apache 2.4 (docker) as a reverse-proxy to distribute different subdomains to different docker services. I redirect http-requests to https using the Redirect directive. One specific URL-path (the part after the domain), however, should not be redicted to https, but served with files from a specific directory. I'm trying to accomplish this using the Alias directive, which does not work.
I'm assuming that Redirect overrides Alias. Is that true?
And how could I accomplish my goal if this is the case?
<VirtualHost *:80>
ServerName service.example.com
Alias /exception/ /var/www/exception
Redirect permanent / https://service.example.com/
</VirtualHost>
I expected this to work, but it does not.
From mod_alias docs:
First, all Redirects are processed before Aliases are processed, and
therefore a request that matches a Redirect or RedirectMatch will
never have Aliases applied. Second, the Aliases and Redirects are
processed in the order they appear in the configuration files, with
the first match taking precedence.
To make sure that /exception/ is not matched, use RedirectMatch which allows regex patterns:
RedirectMatch permanent "^/(?!exception/)(.*)" "https://service.example.com/$1"

Apache: Redirect requests where ServerName doesn't match

I'm relatively new to Apache rewrite rules. What I need to do I think should be relatively easy, but I could use a bit of help.
I have a number of name based virtual hosts defined in my Apache configs, for developers to test new feature branches, and I have a wildcard DNS CNAME setup to direct traffic.
Everything works as it should when a request matches the ServerName in one of the virtual hosts. However if the hostname in the request doesn't match any explicitly defined virtual hosts, it automatically uses the first virtual host.
This is quite confusing, as a developer may think they're accessing the correct virtual host, when in fact they're not.
What I'd like to do, is define a rewrite rule in the first virtual host, so that if the hostname in the request doesn't exactly match the defined "ServerName", it will redirect it.
For example...
If I have 2 virtual hosts defined like so...
<VirtualHost *:443>
ServerName default.mydomain.com
...
</VirtualHost>
<VirtualHost *:443>
ServerName my-feature-1.mydomain.com
...
</VirtualHost>
and a developer is trying to request the site for their new feature, but spell it incorrectly, i.e.
https://feature-1.mydomain.com
The first virtualhost will silently serve the request, and they may be none the wiser, and wonder why their new feature code appears not to be working.
What I would like it to do instead. I'd like to redirect them to...
https://default.mydomain.com
so that it's obvious they've misstyped the URL.
I've got it to work with the following rewrite rule in the first virtual host...
RewriteCond %{HTTP_HOST} "!^default\.mydomain\.com" [NC]
RewriteRule ^/(.*) https://default.mydomain.com [L,NE,R=301]
This works as the default for any request not explicitly matched by the subsequent dynamic virtual hosts but also redirects to the preferred default URL. I don't want to actually serve any content on undefined URL's.
The only thing that would improve it slightly would be to not have to duplicate the domain name in the rules.

How can I redirect non-existing sub-domains to domain [using .htaccess]

Suppose, a user (of a website), types a random, not-existing sub-domain name:
random.example.com
Is it possible to redirct them to the [main] domain:
example.com
The issue is that, if users type [or misspell] a sub-domain name - then currently, it goes to a [404] error page. Yet, I have already placed:
ErrorDocument 404 http://example.com
In the .htaccess - but, it does not redirect. Is it possible to create some kind of rewrite conditions/rules for a wildcard, such as all sub-domains that do not exist?
It seems that the sub-domains's vhost's document root are pointing to somewhere else, so probably you cannot do this.
On the other hand in the main apache config, you could use the ServerAlias directive to catch more domains with regular expressions or if you can control the default virtual host (the first usually), you can use rewrite rules as well.

Using fall-through rewrite for mod_vhost_alias

I run a couple dozen sites on my test VPS, and currently use mod_vhost_alias to avoid needing a new VirtualHost every time I throw up a new site. My current configuration looks like this:
<VirtualHost *:80>
ServerName my.servername.com
ServerAlias *
VirtualDocumentRoot /var/www/%0/public_html
</VirtualHost>
Inside my /var/www directory, each site has its own directory. For instance, the path to my personal page is /var/www/personalsite.com/public_html/index.php. This is working great for requests to http://personalsite.com.
However, this does not work when requests come in for http://www.personalsite.com. For some of my other sites, I have the inverse problem -- the directory may be /var/www/www.sitename.com/public_html, so requests for http://www.sitename.com are fine. However, requests for http://sitename.com do not work.
Is there a way to set up my Apache config so that when a request comes in, it does the following? Are there any performance implications of doing it this way?
In pseudocode:
1. Check if the directory or file exists. If it does, skip the rest of the rules
(but don't stop, in case a local .htaccess has rules in it for pretty URLs
in WordPress or Concrete5)
2. If the file/directory does not exist:
1. If the host header starts with "www":
1. remove the www from the host header and try the first rule again.
2. If the host header does not start with "www":
1. add "www" to the beginning of the host header and try the first rule again
3. If it still fails after trying both conditions:
1. Go to a 404 error page
I'm currently doing this with about 20 virtualhosts, but that seems ridiculous when I have to add a new one for each site. The point of using mod_vhost_alias was to avoid needing all these VirtualHosts in the first place.
Assuming you're OK with redirecting users, you can use one of the techniques from https://stackoverflow.com/a/2361508/881615:
either set a mod_rewrite rule to remove the leading www. from requests,
or set a redirect in each vhost to redirect requests with a leading www. to the top-level domain

help regarding setting up pseudo/fake subdomains on apache

First of all, sorry if I got the term 'pseudo subdomain' wrong.
what I am trying to achieve is this-
When someone registers on my application, they get a new url like..
yourname.myapp.tld
I dont want to use the subdomain system for this. To be frank, I dont know how the subdomains exactly work but it guess it requires a folder per subdomain inside the document root and then the server redirects the requests there.
Can this be achieved by doing something like -
when a visiter types any subdomain, (anything.myapp.tld), he is able to access myapp . In the index.php file i will explode the $_SERVER['HTTP_HOST'] to get the subdomain which i will store in session and will thereafter act as an identifier for that user. Ideally i wouldnt want to create any vhosts or add many lines to the hosts file. Can this be achieved with just one vhost?
Is this possible with mod rewrite or something ?
Yes you can archive this using wildcard that needs to be configured on both, the dns server and http server
On the dns a entry like this (installing dns on ubuntu https://help.ubuntu.com/10.04/serverguide/C/dns.html):
; wildcard subdomains are all directed to this IP
; of course this should be the IP of your web server
*.domain.tld. IN A 1.2.3.4
At apache an entry like this:
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.domain.tld
ServerAlias *.domain.tld
</VirtualHost>
What happens after is that everything.domain.tld will be going to your main folder so you can use the index.php to redirect it to the right place or even an htaccess using mod_rewrite.