rewrite subdomain to direcory in same server - apache

I have a domain, lets say mydomain.com.
I want every subdomain of this to load the contents of the directory with the same name... For example, if someone writes
http://sub.mydomain.com/index.php
I want to show him the contents of
http://sub.mydomain.com/sub/index.php
Still, I want it to show in the addressbar the http://sub.mydomain.com/index.php
The apache vhost is like
<VirtualHost *:80>
ServerName *.mydomain.com
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
so in the filesystem, the files for the example above would be under the directory
/var/www/vhosts/mydomain.com/subdomains/subs/www/sub
I tried many of the proposed solutions here, but most of them were redirects to some other domain/subdomain or end up in a redirect loop :(
tia

You need to add some rewrite rules that will examine the sub domain and then rewrite to the relevant path:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com
RewriteRule ^/(.*)$ /%1/$1
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
What this will do is capture anything preceding ".mydomain.com" then rewrite it into the URL as %1, $1 will be the requested resource such as index.html
Be aware this might trip you up if www.mydomain.com is a valid domain for your site!

Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.[^.]+\.[^.]+$ [NC]
RewriteRule !^sub(|/$) /sub%{REQUEST_URI} [L,NC]

Related

Apache: Using VirtualHost redirect to URL with RewriteCond applied?

So I have a RewriteCond that turns:
domain.com/view.php?a=1
into:
domain.com/artist/name
I have a shorter domain: dmn.com that I would like to provide shortlinks for artists:
dmn.com/name
How can I get dmn.com/name to redirect to: domain.com/artist/name while keeping the shortlink in tact?
.htaccess:
#rewrite profile requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule artist/(.*)$ view.php?a=$1 [QSA,NC,L]
httpd.conf:
<VirtualHost *:80>
DocumentRoot /var/www/html/ // <-- ??? not sure what to put here
ServerName dmn.com
ServerAlias www.dmn.com
</VirtualHost>
Since the directory "/artist" doesn't actually exist (since it's being rewritten), I can't seem to put /var/www/html/artist in the DocumentRoot of the VirtualHost config without Apache spitting back:
Warning: DocumentRoot [/var/www/html/artist] does not exist
I think what you want is not an actual redirection, but another rewrite.
You can't do it with a separate VirtualHost. Well, you can if you point it to the same DocumentRoot and duplicate your rules or load them from a common file, either via .htaccess or a Include directive.
You can also add the short domain name to your main domain ServerAlias
<VirtualHost *:80>
# This should point to your current config
DocumentRoot /var/www/artists_profiles/
ServerName example.com
ServerAlias www.example.com ex.com www.ex.com
</VirtualHost>
Then you add the new rules to the top of the file:
RewriteBase /
RewriteCond %{HTTP_HOST} ex.com
RewriteRule ^(^.*) artist/$1
Then your existing rules should pick up. If you want to avoid the shortened domain from responding to the longer urls you could add a similar RewriteCond to the existing rules:
RewriteCond %{HTTP_HOST} !ex.com

Apache httpd.conf - Link multiple domains to corresponding subfolders

I need a rule to internally rewrite several domains, with and without www:
www.a.com --> /m/n/o/
b.c.org --> /x/y/z/
The setup is Apache running locally on Windows (XAMPP). I've got the hosts file set up so all the domains point to localhost. I'd like every page to get redirected, i.e. I want to point each domain to it's own different root directory and have it work normally from there. e.g.
/ <-- Top level folder, everything is under here.
/root/of/domain/A/ <-- www.a.com
/root/of/domain/C/ <-- b.c.org
You have two choices.
(1) The one you asked (with mod_rewrite)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?a\.com$ [NC]
RewriteRule ^/(.*)$ /root/of/domain/A/$1 [L]
RewriteCond %{HTTP_HOST} ^b\.c\.org$ [NC]
RewriteRule ^/(.*)$ /root/of/domain/C/$1 [L]
</IfModule>
Note: don't forget to replace example values by real ones. Also, make sure mod_rewrite is enabled.
(2) the cleanest way: configure virtualhosts directly (without mod_rewrite)
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "X:/path/to/root/of/domain/A/"
ServerName a.com
ServerAlias www.a.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "X:/path/to/root/of/domain/C/"
ServerName b.c.org
</VirtualHost>

Apache Server RewriteRule to Tomcat WAR

I'm trying to get Apache's RewriteEngine to rewrite some URL's to Tomcat Webapp Directories.
It already works partially, but there is one thing that I cannot achieve.
This is the scenario:
I have apache and a tomcat running on a vps.
On the tomcat I have a webapp called XYZ.
On Apache, I use the jk_mod module with workers.properties to "connect" to the tomcat.
In my httpd.conf I have a VitualHost set up as follows:
<VirtualHost *:80>
ServerName abc.domain.com
RewriteEngine on
RewriteRule ^/(.+)$ /XYZ/$1 [L,PT]
JkMount /* XYZ
</VirtualHost>
So far, so good. Without the RewriteRule I'd have to open the URL like this:
abc.domain.com/XYZ/home (where "home" is the jsp)
abc.domain.com/XYZ/customers
etc...
And the WITH the RewriteRule I'm down to this:
abc.domain.com/home
abc.domain.com/customers
etc...
Which is already pretty good for my goal, but there is just one more thing that needs to be done: I want to be able to open abc.domain.com and to be redirected to the last example above (home jsp).
I've tried adding some other RewriteRules that would rewrite the root to /XYZ/home and also tried a Redirect, but none of those worked as I expected them to (nothing happened)...
Can somebody please explain, how I can achieve this?
Thank you very much in advance!
I got it to work in the end. Using a second RewriteRule. I guess that I had some mistakes in my wildcard / regex in my previous tries.
This is what works:
<VirtualHost *:80>
ServerName abc.domain.com
RewriteEngine on
RewriteRule ^/(.+)$ /XYZ/$1 [L,PT]
RewriteRule ^/$ /XYZ/home [L,PT] # <---- This
JkMount /* XYZ
</VirtualHost>
During my research I also learnt that a Redirect 301 / /home will basically redirect everything, NOT only the root, as would have needed it to.
Put the Redirect above the RewriteEngine clause.
Something like:
<VirtualHost *:80>
ServerName abc.domain.com
Redirect 301 ^/$ /home
RewriteEngine on
RewriteRule ^/(.+)$ /XYZ/$1 [L,PT]
JkMount /* XYZ
</VirtualHost>

Apache Redirect Rule Needed.Thanks!

everyone
I have one problem here.
My site is powered by Mediawiki,
which means, the visitors are supposed to visit links like this:
hxxp://www.example.com/wiki/SOMETHING
I want to redirect the user to the Main Page if he is trying to access something else:
hxxp://www.example.com/NOTwiki/SOMETHING -> hxxp://www.example.com/wiki/Main_Page
and
hxxp://www.example.com/NOTwiki -> hxxp://www.example.com/wiki/Main_Page
And "/wiki" is case sensitive,
which means:
hxxp://www.example.com/WiKi/SOMETHING -> hxxp://www.example.com/wiki/Main_Page
I am using a virtual host like this:
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAdmin admin#example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example/public_html/mediawiki/
ErrorLog logs/AP/error_log
</VirtualHost>
I am new on Apache and still learning.
Be specific,Please.
Thanks a lot!
This config should be:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteCond %{REQUEST_URI} ^index.php
RewriteRule .* - [L]
RewriteRule !^wiki(/.*)?$ wiki/Main_Page [R=301,L]
</IfModule>
The RewriteCond/RewriteRule says "If the start of the path is not /wiki, rewrite all URLS to /wiki/Main_Page using a 301 redirect (R=301), and do not process any more rules (L)"

Redirect/rewrite alternative domains to root domain with apache2/htaccess

I 2 domains with and without the www prefix. When a user visits any of these domains, I want it to automatically reroute to a chosen 1 of them.
For example:
domain.com
www.domain.com
domain.co.uk
www.domain.co.uk
When a user visits www.domain.com, domain.co.uk or www.domain.co.uk, it will rewrite to domain.com
So far, I have my apache2 virtual host block setup like this:
<VirtualHost *:80>
ProxyPass / http://localhost:3060/
ProxyPassReverse / http://localhost:3060/
ServerName domain.com
ServerAlias www.domain.com
ServerAlias domain.co.uk
ServerAlias www.domain.co.uk
</VirtualHost>
But this doesn't do the rewriting/rerouting. I need to also make sure that it takes into account any paths. For example, www.domain.co.uk/test would change to domain.com/test
Any ideas how I can do this in the virtual host block? I'm assuming I would split the 3 domains to be rewritten into a separate block and treat them there, but really not sure how to accomplish all the rules I need.
Per your comment, you want to redirect the three aliases to the main domain, and you've stated that you would like to do this within the virtual host configuration.
<VirtualHost *:80>
...
RewriteEngine on
# If using one of the aliases ...
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk
# ... redirect to the main domain
RewriteRule ^(.*)$ http://domain.com/$1 [R=302,L]
</VirtualHost>
You can also add the Rewrite* directives in your domain's .htaccess file.
To make the redirect permanent, change 302 to 301 - this basically instructs browsers and search engines to cache the redirect.