Using fall-through rewrite for mod_vhost_alias - apache

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

Related

How can I write the Apache configuration two different sites?

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

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.

Unwanted Apache redirect behavior

My Apache installation on my Ubuntu 12.04 server redirects requests addressed to:
http://87.73.120.126
to
http://90.184.18.96/joomla/index.php/jomsocial
The address 87.73.120.126 is my servers current IP-address. The address 90.184.18.96 was the one it had half a year ago.
My question is: why does it do this? and how do I make it stop?
Half a year ago I wanted to make requests to http://90.184.18.96 redirect to http://90.184.18.96/joomla/index.php/jomsocial
I did this by adding this line:
RedirectMatch permanent ^/$ http://90.184.18.96/joomla/index.php/jomsocial
to the file /etc/apache2/apache2.conf
A couple of days ago I tried to solve the current redirect problem by changing the line above to:
RedirectMatch permanent ^/$ http://127.0.0.1/joomla/index.php/jomsocial
and rebooting the server. This did not solve the problem!
Then I tried to do a global file search for the string 90.184.18.96 using this command:
sudo grep 90.184.18.96 / -irn --exclude-dir={bin,lib64,opt,sbin,tmp,boot,lost+found,proc,selinux,dev,media,root,srv,lib,mnt,run,sys,mail,log,doc,src,recovery-mode} --color
this search revealed a couple of outdated CRON-entries that I also fixed to use 127.0.0.1 instead. This didn't help either. So, where could the string '90.184.18.96' be residing? I guess it must be somewhere in order for anything to redirect to that address.
This server runs Joomla from the path /var/www/joomla/. As this path is not excluded by my grep-search any redirection emerging from Joomla should be revealed by the grep-search.
Two considerations:
Redirecting to 127.0.0.1 will redirect to the user's machine, not your server. If you're only spanning one server, you should not include your server's IP (it's not nice to access a site with a domain i.e. www.your-domain.com and be redirected to an ip address). So the rule should use variables for the host: %{HTTP_HOST} and %{REQUEST_URI} for host and path.
Besides httpd.conf you may also have redirect rules in your .htaccess which resides in the webserver root: and it's a hidden file so it could have been skipped by your grep, have a look into that as well. Beware the syntax of .htaccess and httpd.conf is slightly different.
You're achieving two things with that redirect: point to an installation which is off-root (/joomla) and setting a different homepage. Setting the homepage in Joomla is as easy as marking its menu item with a star in the menu item editor, so you might not need the second part.

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.

Apache: Redirect blog.foobar.com to www.foobar.com

I have a site at blog.foobar.com that I have closed down, and I want any
page requested there to be forwarded to www.foobar.com
I want my VirtualHost config to do this for me. I currently have the following lines that does nearly what I want but not exactly:
redirect permanent / http://www.foobar.com
Unfortunately what happens is that if I ask for blog.foobar.com instead of forwarding to www.foobar.com it serves the pages on blog.foobar.com instead.
Is there a way doing this in the VirtualHost config or should I use a .htaccess file instead?
Regards
Steve
You can use the Redirect directive in the context of either a VirtualHost or a .htaccess file. However, what you probably want is a RedirectMatch:
RedirectMatch permanent (.*)$ http://www.foobar.com$1
With that inside your blog.foobar.com VirtualHost, any request to blog.foobar.com would be directed to the same page on www.foobar.com, ie. blog.foobar.com/my/page would go to www.foobar.com/my/page.