Rewrite db.example.com to example.com/db - apache

I've got myself an apache server and I'm trying to learn it's quirks...
I've got DNS to point example.com to 1.2.3.4 and I've set up a vhost on the apache2 server.
Now I want to, in the same vhost, point db.example.com to serv the contents of example.com/db.
I was hoping I could use some kind of mod_rewrite og ServerAlias, but sofar I've hot a wall.
The DNS part is covered, I just can't figure out what to do in apache.

In your .htaccess file, try something among the lines:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^db\.example\.com
RewriteRule (.*) http://example.com/db/$1 [L]

Related

DNS/.htaccess files to redirect subdomain to specific folder

I'm trying to redirect my subdomain to a flder. I think I have the .htaccess code from another post here. Something like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^clients\.example\.com$
RewriteRule ^ http://example.com/clients/root/app%{REQUEST_URI} [L,P]
What DNS settings should I set up for the clients. subdomain to tie it in with the .htaccess.
You need to create a CNAME to point clients.example.com to the same server as example.com. Then you need to make sure your webserver is setup to serve the clients.example.com in the same root as example.com.
Of, you can simply setup clients.example.com's vhost to point to the /clients/root/app/ folder instead. There's a directive called DocumentRoot that you'd use to point a vhost to a where it's root folder is.
In the case of the first instance, where both the main domain and subdomain point to the same root, you don't need to use the P flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^clients\.example\.com$
RewriteRule ^ /clients/root/app%{REQUEST_URI} [L]
Is good enough and circumvents the need to go through mod_proxy.

In Apache how do I redirect all traffic going to scales.ejiw.com to scales.ejco.com?

Our old domain is scales.ejiw.com and our new domain is scales.ejco.com. For awhile I would like to run both in tandem with scales.ejiw.com just redirecting to scales.ejco.com. How do I set this up in Apache? Can someone please point me in the right direction?
I believe the best way to go is with a 301 redirection.
First of all because its search engine friendly and second of all cause its pretty easy to set up.
The best way to this with Apache is to edit the .htaccess file in the document root of your server and add the following lines:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^newdomain\.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
(Change the newdomain.com with your new domain of-course)
This way is will redirect 'www' and non 'www' traffic.
You can read some more about 301 redirections here.

Apache Virtual Host Redirect: *.example.com to www.example.com

I'm simply trying to redirect any example.com to www.example.com. The redirect currently sends example.com to www.example.com//.
The following code is in my virtual host configuration file:
RewriteCond %{HTTP_HOST} ^suksanvillas\.com$ [NC]
RewriteRule ^(.*)$ http://www.suksanvillas.com/$1 [R=301,L]
This seems to be what all the tutorials suggest, but I'm unable to understand how I'm picking up the extra "/" on the redirect. Any help on even where I might look is appreciated.
PS: Other subdomains, for examples "guides.examples.com" should not be redirected.
Thanks.
Avoid capturing the leading slash by using "^/" in the beginning of the pattern:
RewriteRule ^/(.*)$ ...
With that small change everything should work great.

Simple rewrite rule for a nginx + apache2 with mod_wsgi

I'm stuck with this, my skills in the web servers area are poor...
I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, i.e. stripping the www part from each URL request before serving. This is the layout of the different conf files:
=== /etc/nginx/sites-enabled/example.com ===:
http://dpaste.com/82638/
=== /etc/apache2/sites-enabled/example.com ===:
http://dpaste.com/hold/82645/
=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:
http://dpaste.com/82643/
In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That worked perfectly.
But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever, everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:
http://example.com/example.wsgi/
Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:
rewrite ^/(.*) http://example.com/$1 permanent;
but now I am getting what firefox calls a "circular loop"...
So who can I get this (I guess trivial) thing up?
Thanks in advance,
Hector
The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com
server {
listen 80;
server_name www.example.com;
rewrute ^/(.*) http://example.com/$1 permanent;
}
All right I found the solution to avoid the circular loop... by creating TWO server sections in my nginx config file, one for www.example.com -- which has the rewrite rule suggested by rzab -- and the other for example.com, which contains all the rest of directives.

Redirect subdomain to /folder

I want to redirect the sub-domain webmail to /roundcube for that domain. This have to work for all virtual hosts in apache.
Example:
webmail.example.com must point to [www.]example.com/roundcube
How is this possible? The server where it has to be done is configured with direct admin :S
mod_rewrite is your friend.
Try something like this in your Apache VirtualHost configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^webmail\.[^.]+\.[^.]+$
RewriteRule ^webmail\.([^.]+)\.([^.]+)$ http://www.$1.$2/roundcube [R=permanent]
...and configure DNS to point webmail.example.com to the same server as www.example.com.